o = document.getElementsByName("cName")
hasC = 0
for(i = 0;i<o.length;i++)
{
if(o[i].checked) hasC++
}if(hasC == 0)
{
alert("必须选")
return false
}
return true
hasC = 0
for(i = 0;i<o.length;i++)
{
if(o[i].checked) hasC++
}if(hasC == 0)
{
alert("必须选")
return false
}
return true
o[0],如果只有一个的话。
* This function is to get the number of the checked checkboxes|radios.
* JK 2003-12-08
*/
function getCheckedCheckboxesNum(nameOfCheckBox)
{
var theNum=0;
var theCheckboxInputs=document.getElementsByName(nameOfCheckBox);
for (var i=0;i<theCheckboxInputs.length;i++)
{
if(theCheckboxInputs[i].checked) theNum++;
}
return theNum;
}
不是吧,一个也可以的。因为通过getElementsByName得到的 o 是一个数组。所以可以直接应用
o[0],如果只有一个的话。
-------------
验证OK.多学了一招。