Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript checkAll函数_Javascript - Fatal编程技术网

Javascript checkAll函数

Javascript checkAll函数,javascript,Javascript,运行第一个代码段时,通过单击表格标题的复选框,中的复选框数将显示在警报框中。但是,运行第二个代码段时,中的复选框编号将不会显示在警报框中 注意:第一个代码段得到三个表行,第二个代码段得到两个表行 有人能帮我吗?提前谢谢 ------第1段----------------- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transit

运行第一个代码段时,通过单击表格标题的复选框,
中的复选框数将显示在警报框中。但是,运行第二个代码段时,
中的复选框编号将不会显示在警报框中

注意:第一个代码段得到三个表行,第二个代码段得到两个表行

有人能帮我吗?提前谢谢

------第1段-----------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
        function CheckAll(formName,eleName,controlID)
        {
            var count = document.forms[formName].elements[eleName].length;
            alert(count);
        }
    </script>
    </head>

    <body>
    <form name="report" method="post" action="#">

        <table border="1">
            <tr>
                <th width="5%">
                <input type='checkbox' id="selectController" onClick="CheckAll('report','list[]',this.id)"/>
                </th>
                <th width="30%">
                Name
                </th>
                <th width="30%">
                Number
                </th>
                <th width="35%">
                </th>
            </tr>
            <tr id='row0' class='odd' >
                <td >
                <input name='list[]' type='checkbox'  value="1" id='checkbox-1' onClick='ClickCheckbox(this.id)' />
                </td>
                <td>
                SensorA
                </td>
                <td>
                1234567
                </td>
                <td>
                    [ <a href="#" class="basketlink">Add</a> ]
                </td>
            </tr>
            <tr id='row1' class='even' >
                <td >
                <input name='list[]' type='checkbox'  value="2" id='checkbox-2' onClick='ClickCheckbox(this.id)' />
                </td>
                <td>
                SensorA
                </td>
                <td>
                1234567
                </td>
                <td>
                    [ <a href="#" class="basketlink">Add</a> ]
                </td>
            </tr>

        </table>
    </form>
    </body>
    </html>

无标题文件
函数CheckAll(formName、eleName、controlID)
{
var count=document.forms[formName]。元素[eleName]。长度;
警报(计数);
}
名称
数
传感器A
1234567
[  ]
传感器A
1234567
[  ]
--------------第二段片段-----------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
    function CheckAll(formName,eleName,controlID)
    {
        var count = document.forms[formName].elements[eleName].length;
        alert(count);
    }
</script>
</head>

<body>
<form name="report" method="post" action="#">

    <table border="1">
        <tr>
            <th width="5%">
            <input type='checkbox' id="selectController" onClick="CheckAll('report','list[]',this.id)"/>
            </th>
            <th width="30%">
            Name
            </th>
            <th width="30%">
            Number
            </th>
            <th width="35%">
            </th>
        </tr>
        <tr id='row0' class='odd' >
            <td >
            <input name='list[]' type='checkbox'  value="1" id='checkbox-1' onClick='ClickCheckbox(this.id)' />
            </td>
            <td>
            SensorA
            </td>
            <td>
            1234567
            </td>
            <td>
                [ <a href="#" class="basketlink">Add</a> ]
            </td>
        </tr>
    </table>
</form>
</body>
</html>

无标题文件
函数CheckAll(formName、eleName、controlID)
{
var count=document.forms[formName]。元素[eleName]。长度;
警报(计数);
}
名称
数
传感器A
1234567
[  ]

在第二种情况下,只有一种。因此,您不会得到
节点列表
(一种数组),而是元素本身,它没有
.length
属性。因此,如果
.length==undefined
,则假定它是
1

缩小范围示例:

  • 具有1个元素:
  • 有两个要素:
    • 您的问题是“.length”。长度是数组的属性,而不是每个javascript对象的属性。在您的第二个代码段中,由于您只有1个复选框,所以document.forms[formName].elements[eleName]是一个对象而不是数组;此对象没有长度属性。因此,您应该这样做:

          var count;
      if(document.forms[formName].elements[eleName].length)
       count = document.forms[formName].elements[eleName].length;
      else
       count = 1 //only if you are sure that you will always have 1 checkbox rendered, otherwise use some other logic to say 0 or 1.
      

      在旁注中,第一个示例中有一个用
      关闭的
      。是的,第一个有三个输入标记,第二个只有两个。您正在计算表单的元素。