我必須驗證是否檢查了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