-1
我試圖驗證組的兩個性別單選按鈕,但我得到一個錯誤。 我有以下幾點:的Javascript:驗證單選按鈕
這是給在itm.length錯誤;說它是未定義的。
function validateSex(itm, elmt) {
\t var dom = document.getElementById(elmt);
\t var numChecked = 0;
\t for (var index = 0; index < itm.length; index++) {
\t \t \t if (itm[index].checked) {
\t \t \t numChecked++;
\t \t \t break;
\t \t }
\t }
\t // if sex not selected display error message.
\t if (numChecked === 0) {
\t \t dom.innerHTML = "Please select the sex.";
\t \t return false;
\t }else {
\t \t dom.innerHTML = "";
\t \t return true;
\t }
}
<table>
\t <tr>
\t \t <td>
\t \t \t <input type = "radio" name = "sex" value= "male"
\t \t \t \t title = "Select sex"
\t \t \t \t onchange = "validateSex(this, 'msgSex')"/>Male
\t \t \t <input type = "radio" name = "sex" value= "female"
\t \t \t \t title = "Select sex"
\t \t \t \t onchange = "validateSex(this, 'msgSex')"/>Female
\t \t </td>
\t \t
\t \t <td id = "msgSex"></td>
\t </tr>
<table>
做出的jsfiddle更多理解 – prog1011 2014-10-11 04:23:37