2013-03-01 48 views
1

我想这个代码来处理形式:jQuery的.preventDefault()问题

//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; 
}); 

有一对夫妇的我遇到的问题。

1.)当表单被提交时,它调用这个函数。它不调用加载程序,或者如果是这样,它会被控制器的重定向覆盖。表单提交时,我需要阻止通用提交。 .preventDefault()无法正常工作(请参阅下文)。

2.)(远比#1重要)如果ajax胡扯出来,我想得到合法的错误。怎么样?

在.preventDefault() - 当我尝试它,我得到这个错误:未捕获的SyntaxError:意外的标识

感谢您的帮助。

+0

您的x.preventDefault()在事件处理函数回调函数之外,那么您在哪里使用它? – cernunnos 2013-03-01 17:05:13

+0

谢谢cernunnos - 事后我在错误的位置粘贴了它。我修正了这个例子。 – shubniggurath 2013-03-01 17:06:35

+1

@shubniggurath你在这个脚本之上加载jQuery吗? – Jai 2013-03-01 17:09:25

回答

1

你得到的是从别的地方来的异常,请访问此琴:http://jsfiddle.net/DgRLa/

它有这样的代码:

$(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和警报。

使用“成功”的回调,而不是“完全”之一,并且将递延.fail捕获错误,请尝试这样的事:

$(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:你提到装载机向用户发送到不同的页面,如果你打算重定向页面,是否有理由提交带有Ajax的表单?

+0

谢谢!我相信我找到了罪魁祸首。 – shubniggurath 2013-03-01 17:17:38

+0

只是一个更新 - 你的例子有巨大的帮助。再次感谢! – shubniggurath 2013-03-07 13:24:48

+1

没问题:)如果你想了解更多关于jQuery ajax函数成功/失败/进度的信息,我建议你阅读jQuery Deferred对象(http://api.jquery.com/category/deferred-object/) 。当你在javascript中使用异步时,有很好的信息。 – cernunnos 2013-03-07 14:49:30

1
$(document).on('submit', 'form', function (x) { 
    x.preventDefault(); //x must be inside the anonymous function 
    $.ajax({ 
+0

哎呀,我把preventDefault粘贴在错误的地方。这正如我所尝试的一样。编辑:修正上面。 – shubniggurath 2013-03-01 17:04:43