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

解决方案 »

  1.   

    如果不考虑只有一个 checkbox 的时候 楼上的为正解。
      

  2.   

    不是吧,一个也可以的。因为通过getElementsByName得到的 o 是一个数组。所以可以直接应用
    o[0],如果只有一个的话。
      

  3.   

    /**
    * 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;
    }
      

  4.   

    -------------
    不是吧,一个也可以的。因为通过getElementsByName得到的 o 是一个数组。所以可以直接应用
    o[0],如果只有一个的话。
    -------------
    验证OK.多学了一招。