Демо: http://jsfiddle.net/rBaUM/16/
function onSubmitForSomeForm() {
var groups = {},
group;
[].forEach.call(this.elements, function (input) {
if (input.type === "radio" && input.name) {
groups[input.name] = groups[input.name] || [];
groups[input.name].push(input.checked);
}
});
for (group in groups) {
if (!groups[group].some(function (checked) {
return checked;
})) {
return false; //Some group was completely unchecked
};
}
return true;
}
console.log( "Both groups completely unchecked", onSubmitForSomeForm.call( document.myform ) );
document.myform.check_list2[0].checked = true;
console.log( "1st group completely unchecked", onSubmitForSomeForm.call( document.myform ) );
document.myform.check_list2[0].checked = false;
document.myform.check_list[0].checked = true;
console.log( "2nd group completely unchecked", onSubmitForSomeForm.call( document.myform ) );
document.myform.check_list2[0].checked = true;
console.log( "Both groups have checked checkbox", onSubmitForSomeForm.call( document.myform ) );
Необходимо подложить .forEach и .some
Для использования:
<form onsubmit="return onSubmitForSomeForm.call(this);"></form>
Или:
myform.onsubmit = onSubmitForSomeForm
Или:
myform.addEventListener( "submit", onSubmitForSomeForm, false );