问题描述
我在链接上使用单击功能以通过ajax提交表单,如果服务器返回错误,则会显示服务器端错误.
I'm using a click function on a link to submit the form via ajax, and if the server returns with an error, a server side error is displayed.
使用此方法,enter键不能在表单中使用,我认为可能存在一种更简单,更正确的方法来使用jquery验证的提交处理程序.
With this method, the enter key doesn't work in the form, and I think there's probably an easier and more correct way to do this with jquery validation's submit handler.
到目前为止,这是我的代码...
here's my code so far...
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');
}
}
});
}
和点击功能:
$("a#loginButton").click(function(){
$("#loginForm").validate().form(this);
if($('#loginForm').valid()){
loginSubmit();
}
});
任何帮助将不胜感激.
Any help would be appreciated.
推荐答案
您需要在输入字段上听ENTER键的输入:
You'll want to listen for the ENTER keypress on your input fields:
$('input').keypress(function(event) {
if(event.which == 13) {
loginSubmit();
event.preventDefault();
return false;
}
});
当然,您可能不想只捕获所有INPUT标记上的ENTER,因此您可以根据需要使用其他选择器.
Of course you might not want to capture ENTER on all your INPUT tags, just some, so you could use another selector depending on your needs.
在此处查看类似的讨论.
这篇关于jQuery验证提交-使用Ajax提交-单击功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!