Do not use ajax to implement the submit form without refreshing

  • 2020-05-07 19:13:45
  • OfStack

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">
    Here is the form content
</form>

JS code:

If saved successfully, 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=" + '@ViewBag.roadId' + "&roadName=" + '@ViewBag.roadName' + "&viewOrEdit=1&t=" + new Date().valueOf());
        } else {
            alert(" Save failed: " + data.msg);
        }
    });
}

Is it very simple? If you need it, just take it away. You are welcome to ^_^


Related articles: