Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery.preventDefault()问题_Jquery_Ajax_Forms_Form Submit - Fatal编程技术网

jquery.preventDefault()问题

jquery.preventDefault()问题,jquery,ajax,forms,form-submit,Jquery,Ajax,Forms,Form Submit,我希望使用以下代码处理表单: //All form submissions should go through here. $(document).on('submit', 'form', function (x) { //x.preventDefault(); //commented out because it's not working $.ajax({ url: $(this + '#formdestination').val(), //for

我希望使用以下代码处理表单:

 //All form submissions should go through here.
 $(document).on('submit', 'form', function (x) {
     //x.preventDefault();  //commented out because it's not working
     $.ajax({
         url: $(this + '#formdestination').val(), //formdestination is a hidden field, showing where to submit the form
         type: 'post',
         dataType: 'json',
         data: $(this).serialize(),
         complete: function (xhr, status) {
            if (status === 'error' || !xhr.responseText) {
                window.location = '/broken/?err=Inconsolable%20onsubmit%20ajax%20error'; //i'd like the real error here, ideally
            } //end if
            else {
                loader($('#pagedestination').val()); 
                //loader is a function that animates the page as it loads new content.
                //#pagedestination is a hidden field that stores where the page should go when it's done.
                //Yes, I know formdestination and pagedestination could be handled with the form or the backend.  There are reasons that do not matter much here.
            } //end else
        } //end complete function

    });

    return false;
});
$(document).on('click', '.test', function (x) {
    x.preventDefault();  //commented out because it's not working

    $.ajax({
         url: "test.php", //formdestination is a hidden field, showing where to submit the form
         type: 'post',
         dataType: 'json',
         data: $(this).serialize(),
         complete: function (xhr, status) {
             console.log("hi");
         } 
    }).fail(function () {
        alert("error");
    });
});
我有几个问题

1.)提交表单时,它调用此函数。它不调用loader,或者如果调用了,它将被控制器的重定向覆盖。在提交表单时,我需要防止一般提交。preventDefault()工作不正常(请参见下文)

(远没有#1重要)如果ajax失败了,我希望得到合法的错误。怎么做

On.preventDefault()-当我尝试它时,会出现以下错误:未捕获的SyntaxError:意外标识符


谢谢您的帮助。

您从其他地方得到的例外情况,请访问此小提琴:

$(document).on('submit', 'form', function (x) {
    x.preventDefault();  //x must be inside the anonymous function
    $.ajax({
它有以下代码:

 //All form submissions should go through here.
 $(document).on('submit', 'form', function (x) {
     //x.preventDefault();  //commented out because it's not working
     $.ajax({
         url: $(this + '#formdestination').val(), //formdestination is a hidden field, showing where to submit the form
         type: 'post',
         dataType: 'json',
         data: $(this).serialize(),
         complete: function (xhr, status) {
            if (status === 'error' || !xhr.responseText) {
                window.location = '/broken/?err=Inconsolable%20onsubmit%20ajax%20error'; //i'd like the real error here, ideally
            } //end if
            else {
                loader($('#pagedestination').val()); 
                //loader is a function that animates the page as it loads new content.
                //#pagedestination is a hidden field that stores where the page should go when it's done.
                //Yes, I know formdestination and pagedestination could be handled with the form or the backend.  There are reasons that do not matter much here.
            } //end else
        } //end complete function

    });

    return false;
});
$(document).on('click', '.test', function (x) {
    x.preventDefault();  //commented out because it's not working

    $.ajax({
         url: "test.php", //formdestination is a hidden field, showing where to submit the form
         type: 'post',
         dataType: 'json',
         data: $(this).serialize(),
         complete: function (xhr, status) {
             console.log("hi");
         } 
    }).fail(function () {
        alert("error");
    });
});
单击“clickme”元素时,您将获得console.log和警报

使用“成功”回调而不是“完成”回调,并使用“延迟”回调。如果无法捕获错误,请尝试以下操作:

$(document).on('submit', 'form', function (x) {
     x.preventDefault();  //commented out because it's not working
     var url = $(this).attr("action");     

     $.ajax({
         url: url , // Use the form action attribute instead of a hidden field
         type: 'post',
         dataType: 'json',
         data: $(this).serialize(),
         success: function (data) {
             loader($('#pagedestination').val()); 
         } 
    }).fail(function (error) {
         // error has all sorts of fun stuff
    });

    // return false; Dont return false, preventDefault is enough
});

PS:您提到loader将用户发送到另一个页面,如果要重定向页面,是否有理由使用ajax提交表单?

Oops,我将preventDefault粘贴到了错误的位置。这正是我所尝试的。编辑:修复了上面的问题。您的x.preventDefault()在事件处理程序回调函数之外,这就是您在哪里使用它的方式吗?谢谢Cernunos-我在事后将它粘贴到了错误的位置。我修复了这个例子。@shubniggurath您是否在这个脚本上面加载jQuery?您是否确实确认正在调用该函数?是的。它被称为调整更新-您的示例提供了巨大的帮助。再次感谢!没问题:)如果您想了解更多关于jQueryAjax函数的成功/失败/进展的信息,我建议您阅读jQueryDeferred对象()。在javascript中使用async时,可以获得很好的信息。