jqueryvalidation提交 – 使用ajax提交 – 单击function

我在链接上使用单击function通过ajax提交表单,如果服务器返回错误,则显示服务器端错误。

使用这种方法,回车键在表单中不起作用,我认为使用jqueryvalidation的提交处理程序可能有一种更简单,更正确的方法。

到目前为止这是我的代码……

function loginSubmit(){ $.ajax({ url: loginFormURL, type: 'POST', /** contentType: "application/json;", **/ dataType:"text json", /** async: true, **/ data:$('#loginForm').serialize(), success: function(data){ if(data.isError == "false" || data.isError == ""){ $.postMessage( 'redirectURL|'+ redirectURL +'', '*', parent ); } else if(data.isError == "true"){ $("#loginError").empty().show(); // iterate over the message list and display the error for (var i=0; i < data.errorMessages.length; i++){ // inject error message to the error container $("#loginError").append(data.errorMessages[i]); } // iterate over the field list and paint the fields in red $('input').parent('div').addClass('inputError'); } } }); } 

和点击function:

  $("a#loginButton").click(function(){ $("#loginForm").validate().form(this); if($('#loginForm').valid()){ loginSubmit(); } }); 

任何帮助,将不胜感激。

您需要在输入字段上侦听ENTER键:

 $('input').keypress(function(event) { if(event.which == 13) { loginSubmit(); event.preventDefault(); return false; } }); 

当然,您可能不希望在所有INPUT标签上捕获ENTER,只是一些,因此您可以根据需要使用其他选择器。

在这里看到类似的讨论。