Home >Web Front-end >JS Tutorial >Submit form without refresh without using ajax

Submit form without refresh without using ajax

高洛峰
高洛峰Original
2016-12-16 15:30:471097browse

HTML code:

<iframe id="fra" name="frm" style="display: none;"></iframe>
<form id="frm" method="post" action="@Url.Content("~/Backstage/MachineMng/RoadSet/SaveTrackRoad")?roadId=@ViewBag.roadId" target="frm">
    这里是表单内容
</form>

JS code:

If the save is successful, the background returns {"ok":true}; if the save fails, the background returns {"ok":false, "msg":"error message"}

function save() {
    $("#frm").submit();
    $("#fra").one("load", function () {
        var data = eval("(" + $("#fra").contents().find("body").html() + ")");
        if (data.ok) {
            $("#divtrackroad").load("TrackRoad?roadId=" + &#39;@ViewBag.roadId&#39; + "&roadName=" + &#39;@ViewBag.roadName&#39; + "&viewOrEdit=1&t=" + new Date().valueOf());
        } else {
            alert("保存失败:" + data.msg);
        }
    });
}

Isn’t it very simple? If friends need it, just take it away, you’re welcome^_^


For more related articles on how to implement refresh-free submission forms without using ajax, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn