我想將新規則應用到我當前的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 [email protected]</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]
,group[20353][2]
,group[20353][3]
第3組又名mark3
的組成如下:group[20357][1]
,group[20357][2]
,group[20357][3]
我問題在於規則不起作用,會觸發名稱,電子郵件,但在未選中每個組時都不會觸發複選框。
我該如何解決這個問題?
您的問題描述不清。 **默認**,將'required'規則應用於一組複選框將確保至少有一個被選中。請參閱:http://jsfiddle.net/XqPn2/ – Sparky
請顯示錶格的相關HTML。 – Sparky