JavaScript - CheckBox 全选与取消全选

时间:2022-03-26 09:08:57

JavaScript - CheckBox 全选与取消全选
前言

  提供 CheckBox 全选与取消全选

程序


<script language="JavaScript">
function chkall(input1,input2)
{
    var objForm = document.forms[input1];
    var objLen = objForm.length;
    for (var iCount = 0; iCount < objLen; iCount++)
    {
        if (input2.checked == true)
        {
            if (objForm.elements[iCount].type == "checkbox")
            {
                objForm.elements[iCount].checked = true;
            }
        }
        else
        {
            if (objForm.elements[iCount].type == "checkbox")
            {
                objForm.elements[iCount].checked = false;
            }
        }
    }
}
</script>

<form id="form1">
<input type="checkbox" value='全部選取' onclick='chkall("form1",this)' name=chk><BR>
<input type="checkbox" name="item_001" value="1">1<BR>
<input type="checkbox" name="item_002" value="2">2<BR>
<input type="checkbox" name="item_003" value="3">3<BR>
<input type="checkbox" name="item_003" value="4">4
</form>