我正在尝试将新规则应用到我当前的 jQuery 验证中,该规则将检查我的每个组是否至少选中了 1 个复选框。
我的困难是我无法更改复选框名称。
这是我当前的 jQuery:
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('#docContainer').validate({ // initialize the plugin
rules: {
EMAIL: {
required: true,
email: true
},
FNAME: {
required: true
},
POSTCODE: {
digits: true,
minlength: 4
},
mark1: {
required: function(elem)
{
if ($('input[name="group[20349][1]"]:checked').length > 0 ||
$('input[name="group[20349][2]"]:checked').length > 0 ||
$('input[name="group[20349][3]"]:checked').length > 0)
{
return true;
}
return false;
}
},
mark2: {
required: function(elem)
{
if ($('input[name="group[20353][1]"]:checked').length > 0 ||
$('input[name="group[20353][2]"]:checked').length > 0 ||
$('input[name="group[20353][3]"]:checked').length > 0)
{
return true;
}
return false;
}
},
mark3: {
required: function(elem)
{
if ($('input[name="group[20357][1]"]:checked').length > 0 ||
$('input[name="group[20357][2]"]:checked').length > 0 ||
$('input[name="group[20357][3]"]:checked').length > 0)
{
return true;
}
return false;
}
}
},
messages: {
FNAME: "<b style='color:#FFFFFF'>Please fill your first name...</b>",
EMAIL: {
required: "<b style='color:#FFFFFF'>We need your email address to contact you!</b>",
email: "<b style='color:#FFFFFF'>Your email address must be in the format of name@domain.com</b>"
},
POSTCODE: {
digits: "<b style='color:#FFFFFF'>Post Code must be numbers only...</b>",
minlength: "<b style='color:#FFFFFF'>Post Code must be at least 4 digits...</b>"
},
mark1: "<b style='color:#FFFFFF'>You must CHECK at least 1 option for Type Of Property...</b>",
mark2: "<b style='color:#FFFFFF'>You must CHECK at least 1 option for Bedrooms...</b>",
mark3: "<b style='color:#FFFFFF'>You must CHECK at least 1 option for Price Range...</b>"
}
});
});
</script>
mark1
, mark2
,mark3
是创建的字段以适合每个组的规则,正如您在里面看到的那样,我的复选框对每个组都有唯一的名称,我无法更改它。
第 1 组又名mark1
由:group[20349][1]
、、group[20349][2]
第group[20349][3]
2 组又mark2
由:group[20353][1]
、、、
第 3 组又由:group[20353][2]
、、、group[20353][3]
mark3
group[20357][1]
group[20357][2]
group[20357][3]
我的问题是该规则不起作用,它会触发姓名、电子邮件,但不会触发复选框,因为每个组都没有选中任何复选框。
我怎样才能解决这个问题 ?