使用JQuery验证具有不同保存和提交规则的相同表单元素
我正在尝试使用JQuery验证插件验证表单字段。Im使用jQuery1.10.2。有很多关于这个的帖子,但是没有一个能帮助我实现我试图实现的解决方案。因此提出这个问题。我想在保存和提交时验证表单。我已经在submit的每个元素上定义了一组规则。它工作得很好。现在,单击save按钮,我只需要从验证规则中删除“required:true”。为保存和提交定义的其余验证规则相同 当我尝试上述更改时,我总是得到“对象不支持属性或方法‘规则’”。我在使用IE9(我们在我们的项目中使用IE。需要让它在IE中工作) 代码如下:使用JQuery验证具有不同保存和提交规则的相同表单元素,jquery,jquery-validate,save,submit,Jquery,Jquery Validate,Save,Submit,我正在尝试使用JQuery验证插件验证表单字段。Im使用jQuery1.10.2。有很多关于这个的帖子,但是没有一个能帮助我实现我试图实现的解决方案。因此提出这个问题。我想在保存和提交时验证表单。我已经在submit的每个元素上定义了一组规则。它工作得很好。现在,单击save按钮,我只需要从验证规则中删除“required:true”。为保存和提交定义的其余验证规则相同 当我尝试上述更改时,我总是得到“对象不支持属性或方法‘规则’”。我在使用IE9(我们在我们的项目中使用IE。需要让它在IE中工
$(document).ready(function() {
$("#myForm").formValidate();
});
jQuery.fn.formValidate = function(){
var form = this;
// Set the error to null
$("#errSummary").val('');
$(form).validate({
//Not to place the error messages in line with the text fields
errorPlacement: $.noop,
//Create error summary that will appear before the form
showErrors: function(errorMap, errorList) {
if (errorList) {
var $errorFormId = 'errors-' + form.attr('id');
//Reset and remove error messages if the form has been validated once already
//summary = "";
// Set the summary to original error messages stored as part of submit
// This helps to retain the error messages until there is a new submit
summary = $("#errSummary").val();
//$('label .error', form).remove();
//Create container for error msg display
if($('#' + $errorFormId).length == 0) {
$('<div id="' + $errorFormId + '"/>').insertAfter("#header");
}
//Generate error summary list
if (summary.length == 0){
for (error in errorList) {
//get associated label text to be used for the error summary
var $errorLabel = $('label[for="' + $(errorList[error].element).attr('id') + '"]').text();
summary += '<li><a href="#' + errorList[error].element.id + '" name="' + errorList[error].element.name + '" class="errorLink">' + $errorLabel + ' ' + errorList[error].message + '</a></li>';
}
//Output error summary and place it in the error container
if(summary) {
$('#' + $errorFormId).html('<h2>Errors found in the form</h2><p>One or more errors found in form. Please check and correct the following:</p><ul>' + summary + '</ul>').css("margin-left", "5px");
} else {
$('#' + $errorFormId).html('');
}
// Set the errSummary to current error messages for the most recent submit
$("#errSummary").val(summary);
}
//Move the focus to the associated input when error message link is triggered
$('#' + $errorFormId + ' a').click(function() {
$($(this).attr('href')).focus();
return false;
});
}
this.defaultShowErrors();
submitted = false;
},
invalidHandler: function(form, validator){
submitted = true;
},
onkeyup: false,
onfocusout: false
});
var saveRules = {
field1: {
required: false,
digits: true,
minlength: 7,
maxlength: 10
}
};
var submitRules = {
field1: {
required: true,
digits: true,
minlength: 7,
maxlength: 10
}
};
function addRules(rulesObj){
for (var item in rulesObj){
$('#'+item).rules('add',rulesObj[item]);
}
}
function removeRules(rulesObj){
for (var item in rulesObj){
$('#'+item).rules('remove');
}
}
$("input[type='submit']").click(function() {
if ($(this).attr('id') == "saveForm") {
removeRules(submitRules);
addRules(saveRules);
} else {
removeRules(saveRules);
addRules(submitRules);
}
});
$(文档).ready(函数(){
$(“#myForm”).formValidate();
});
jQuery.fn.formValidate=函数(){
var form=此;
//将错误设置为null
$(“#errSummary”).val(“”);
$(表单)。验证({
//不要将错误消息与文本字段对齐
错误位置:$.noop,
//创建将显示在窗体之前的错误摘要
错误:功能(错误映射、错误列表){
如果(错误列表){
var$errorFormId='errors-'+form.attr('id');
//如果表单已验证一次,则重置并删除错误消息
//summary=“”;
//将摘要设置为作为提交的一部分存储的原始错误消息
//这有助于保留错误消息,直到有新的提交
summary=$(“#errSummary”).val();
//$('label.error',form.remove();
//为错误消息显示创建容器
if($('#'+$errorFormId).length==0){
$(“”).insertAfter(“#标题”);
}
//生成错误摘要列表
如果(summary.length==0){
for(错误列表中的错误){
//获取用于错误摘要的关联标签文本
var$errorLabel=$('label[for=“”+$(errorList[error].element).attr('id')+“]')).text();
摘要+='';
}
//输出错误摘要并将其放入错误容器中
如果(摘要){
$(“#”+$errorFormId).html('表格中发现的错误表格中发现的一个或多个错误。请检查并更正以下内容:'+summary+'
).css(“左边距”,“5px”);
}否则{
$('#'+$errorFormId.html('');
}
//将最近提交的errSummary设置为当前错误消息
$(“#摘要”).val(摘要);
}
//触发错误消息链接时,将焦点移动到关联的输入
$('#'+$errorFormId++'a')。单击(函数(){
$($(this.attr('href')).focus();
返回false;
});
}
this.defaultRors();
提交=错误;
},
invalidHandler:函数(表单、验证器){
提交=真实;
},
onkeyup:false,
onfocusout:false
});
var saveRules={
字段1:{
必填项:false,
数字:对,
最小长度:7,
最大长度:10
}
};
var submitRules={
字段1:{
要求:正确,
数字:对,
最小长度:7,
最大长度:10
}
};
函数addRules(rulesObj){
对于(规则中的var项目){
$('#'+item).rules('add',rulesObj[item]);
}
}
函数删除规则(rulesObj){
对于(规则中的var项目){
$('#'+项).rules('remove');
}
}
$(“输入[type='submit']”)。单击(函数(){
if($(this.attr('id')==“saveForm”){
去除剂(亚微粒);
addRules(saveRules);
}否则{
清除规则(保存规则);
addRules(submitRules);
}
});
此错误是在所有浏览器中发生还是仅在IE中发生?此错误仅在IE中发生