我正在使用jquery验证插件验证表单。我正在检查给定的用户名是否已经在远程选项的db中。一切都像预期的那样工作,但我想在验证脚本请求服务器时显示加载gif。有没有简单的方法来完成这个或我需要编辑插件js文件?无论如何,这里是代表性代码..
rules: {
user_name: {
alfanum:true,//custom method to check if alphanumeric
required: true,
minlength: 5,
remote: {url: "checkuser_custom.php",type:"GET",data:{cmd:"check"}}
}}
所以基本上我想在readystate 2期间显示ajax加载gif
任何帮助赞赏!
根据http://docs.jquery.com/Plugins/Validation/Methods/remote,远程参数是一个完整的jQuery.ajax设置对象。有关实施细节,请参阅http://api.jquery.com/jQuery.ajax。
您可以添加如下方法:
$.validator.addMethod("fullRemote", function(value, element, params) {
if ( this.optional(element) )
return "dependency-mismatch";
var previous = this.previousValue(element);
if (!this.settings.messages[element.name] )
this.settings.messages[element.name] = {};
previous.originalMessage = this.settings.messages[element.name].remote;
this.settings.messages[element.name].remote = previous.message;
if( params.onStart ) params.onStart(element);
params = typeof params == "string" && {url:params} || params;
if ( this.pending[element.name] ) {
return "pending";
}
if ( previous.old === value ) {
return previous.valid;
}
previous.old = value;
var validator = this;
this.startRequest(element);
var data = {};
data[element.name] = value;
$.ajax($.extend(true, {
url: params,
mode: "abort",
port: "validate" + element.name,
dataType: "json",
data: data,
success: function(response) {
validator.settings.messages[element.name].remote = previous.originalMessage;
var valid = response === true;
if( params.onFinish ) params.onFinish(element, valid);
if ( valid ) {
var submitted = validator.formSubmitted;
validator.prepareElement(element);
validator.formSubmitted = submitted;
validator.successList.push(element);
validator.showErrors();
} else {
var errors = {};
var message = response || validator.defaultMessage( element, "remote" );
errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
validator.showErrors(errors);
}
previous.valid = valid;
validator.stopRequest(element, valid);
}
}, params));
return "pending";
}, "The value you entered is already in use.");
然后使用它几乎与“远程”相同,如下所示:
...
rules: {
email: {
required: true,
email: true,
fullRemote: {
url: "../emailavailable",
type: "post",
onStart: function(e){
var loading = $('#ajaxLoading');
loading.appendTo($(e).parent());
alert($(e).val());
},
onFinish: function(e, valid){
var loading = $('#ajaxLoading');
loading.remove();
return true;
}
}
}
},
messages: {
email: {
fullRemote: "email address is already in use."
}
},
...
$(document).ready(function() {
$(document).ajaxComplete(function(event, request, settings) { loading_hide(); });
function loading_show() { $('#modelLoad').removeClass('hide'); }
function loading_hide() { $('#modelLoad').addClass('hide'); }
$("#FrmUser").validate({
rules: {
username: { required: true },
email: {
required: true, email: true,
remote: {
url: '../emailavailable',
type: "post",
data: {
username: function() {
return $("#username").val();
}
} //if it is required
},
beforeSend: function(xhr) {
loading_show();
}
}
}, messages: { email: { remote: "email address is already in use." } }
});//end validation
});//end dom