修改jquery的remote让前段显示服务器错误信息
remote: function (value, element, param) {
//param 是你的rules中规定的参数,在这里肯定是remote中的参数了
//value是你输入的值
//element是你操作的元素,标签
if (this.optional(element)) {
return "dependency-mismatch";
}
var previous = this.previousValue(element),
validator, data;
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;
param = typeof param === "string" && {url: param} || param;
if (previous.old !== value) {
previous.old = value;
validator = this;
this.startRequest(element);
data = {};
data[element.name] = value;//输入框中的数据
$.ajax($.extend(true, {
url: param,
mode: "abort",
port: "validate" + element.name,
dataType: "json",
data: data,
context: validator.currentForm,
success: function (response) {
var valid = response['value'] === true || response['value'] === "true", errors, message, submitted;
validator.settings.messages[element.name].remote = previous.originalMessage;
if (valid) {
submitted = validator.formSubmitted;
validator.prepareElement(element);
validator.formSubmitted = submitted;
validator.successList.push(element);
delete validator.invalid[element.name];
validator.showErrors();
} else {
errors = {};
message = response['msg'] || validator.defaultMessage(element, "remote");
errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
validator.invalid[element.name] = true;
validator.showErrors(errors);
}
previous.valid = valid;
validator.stopRequest(element, valid);
}
}, param));
return "pending";
}
}
作者:百里求一
出处:http://www.cnblogs.com/bergus/
我的语雀: https://www.yuque.com/barry.bai
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。