boj*_*oje 1 validation ajax jquery
我已经完成了这个功能,看起来工作得很好,但它返回了"#",因为它不适合AJAX.我试图删除返回调用,看看.
它需要等到AJAX完成后再返回.JSP工作正常.
怎么了?
jQuery.validator.addMethod("knidExist", function(value, element) {
var valid = "#";
$.ajax({
async: false,
type: "POST",
url: "USER.jsp",
data: "knid="+value,
dataType: "html",
success: function(msg) {
// if the user exists, it returns a string "true"
if($.trim(msg) != "true") {
//return false;
valid = false; // already exists
} else {
//return true;
valid = true; // username is free to use
}
}
});
return valid;
}, 'This USER does not exist');
Run Code Online (Sandbox Code Playgroud)
您需要使用内置的remote验证方法,而不是添加自己的验证方法,而是专门为此目的而设计的.
你会以这样的方式使用它
remote: {
type: "POST",
url: "USER.jsp",
data: { knid: value },
}
Run Code Online (Sandbox Code Playgroud)
查看文档链接到的示例.