Form OnSubmit to wait jQuery Ajax Return?

给你一囗甜甜゛ 提交于 2019-11-29 12:44:26

Since AJAX is asynchronous your validation Would work better using a click handler on the submit button.

Following is based on removing the inline onSubmit

$(function() {

    var $form = $('#myForm');

    $form.find('input[type="submit"]').click(function() {
        $.ajax({
           /* async: false,  this is deprecated*/
            type: "POST",
            url: "ajax.php",
            data: {
                myString: $("#myString").val()
            }
        }).success(function(response) {
            alert(response); //Got 'ok'
            if(response == "ok") {
             /*  submit the form*/
                $form.submit();
            } else {
                alert("Oh, string is wrong. Form Submit is cancelled.");
            }
        }); /* prevent default when submit button clicked*/
        return false;

    });
});

Return value at mark 1 is for your success function of ajax request, not for your validation function. Therefore even if it returns it returns only from there and not from validation function which is mark 2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!