我必须验证是否检查了radiobox。Javascript radiobutton在FF和Chrome中工作,但不在IE中
HTML
<input style="width:20px;" id="radio1" type="radio" name="benvoor" class="benvoor" value="Ik ben voor" /> <label for="radio1">Ik ben voor. </label><br />
<input style="width:20px;" id="radio2" type="radio" name="benvoor" class="benvoor" value="Ik ben tegen" /> <label for="radio2">Ik ben tegen.</label>
的JavaScript/jQuery的
//Assume no radio checked first
var benvoor = false;
for (i in aanmeldform.benvoor) {
// Is the element checked?
if (aanmeldform.benvoor[i].checked) {
//Choice has been made
benvoor = true;
// Cancel the loop if the checked element is found
break;
}
}
// If no choice has been made, put it in the errorList
if (!benvoor) errorList.push("benvoor");
// Cancel submit if errors are found
if (errorList.length > 0) {
document.getElementById("errorMessage").innerHTML = "Graag uw keuze maken";
$("#radiobutton label").addClass("rood");
$("html, body").animate({
scrollTop: $(this).offset().top
}, 1000);
return false;
}
+1。是的,从不使用'for..in'来迭代类似数组的对象(即具有'.length'属性和数字索引的对象)。一个简单的for循环或jQuery的$ .each()是要走的路(或['Array.forEach()'](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/ Array/forEach)如果你有一个实际的数组,而不仅仅是一个类似数组的对象)。 – nnnnnn 2012-03-24 23:49:21