我有兩個性別複選框。男性和女性。我應該如何爲至少一個性別設置我的驗證。從星期一到星期日,我還有一週的時間。這也是至少有一天的選擇。這裏是我的jQuery自定義有效性至少選中一個複選框jquery
$(document).ready(function(){
// --- set required message --- //
var msg="";
var elements = document.getElementsByTagName("INPUT");
for (var i = 0; i < elements.length; i++) {
elements[i].oninvalid =function(e) {
if (!e.target.validity.valid) {
switch(e.target.name){
case 'gender' :
e.target.setCustomValidity("Please select at least one gender.");break;
case 'days' :
e.target.setCustomValidity("Please select at least one day.");break;
case 'location' :
e.target.setCustomValidity("Please enter a location where the image can be seen.");break;
default : e.target.setCustomValidity("");break;
}
}
};
elements[i].oninput = function(e) {
e.target.setCustomValidity(msg);
};
}
});
<input type=checkbox name=gender[] required>Male
<input type=checkbox name=gender[] required>Female
<input type=checkbox name=days[] required>Monday
<input type=checkbox name=days[] required>Tuesday
<input type=checkbox name=days[] required>Wednesday
<input type=checkbox name=days[] required>Thursday
<input type=checkbox name=days[] required>Friday
<input type=checkbox name=days[] required>Saturday
<input type=checkbox name=days[] required>Sunday
性別不是一個廣播組更可能,雖然現在adays誰知道..;] – webkit
@webkit是的,我知道。但在我的網絡上。有一個選擇,男性只能看到和女性。或者可以是兩者。所以我做了一個複選框,以便可以選擇性別。與哪天可以查看的日期相同 – Vincent