JavaScript 中的 HTML 控件数组 - 1 的复选框数组返回零长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17213002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:30:24  来源:igfitidea点击:

HTML Control Array in JavaScript - Checkbox array of 1 returns zero length

javascripthtmlarrayscheckboxzero

提问by Patrick

I'm using a control array of checkboxes to capture a multiple selection

我正在使用复选框控件数组来捕获多个选择

The following code, with two checkboxes, works well and returns a value of 2as expected (or however many are there).

以下带有两个复选框的代码运行良好,并按预期返回值2(或无论有多少)。

However if there is only one checkbox item in the array, it returns a length of 0 (zero).... why is this? Shouldn't it return a length of 1?

但是,如果数组中只有一个复选框项,它返回的长度为0(零)......这是为什么?它不应该返回长度为 1 吗?

I have tried this in Internet Explorer and Chrome with the same results. As a work around I am having to include a hidden bogus checkbox in the array to make sure there is always two or more items when I run the code.

我已经在 Internet Explorer 和 Chrome 中尝试过,结果相同。作为一种解决方法,我必须在数组中包含一个隐藏的虚假复选框,以确保在运行代码时始终有两个或多个项目。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title></title>
    <script language="javascript" type="text/javascript">
        function categoryOnClick() {
            var selectedRows = document.searchForm.elements['categorySelect[]'];
            alert(selectedRows.length);
        }
    </script>
</head>
<body>
    <form name="searchForm" action="">
        <input type="checkbox" name="categorySelect[]" id="1" onclick="categoryOnClick();"/>
        <input type="checkbox" name="categorySelect[]" id="2" onclick="categoryOnClick();"/>
    </form>
</body>
</html>

Code that returns 0 (zero) length...

返回 0(零)长度的代码...

<form name="searchForm" action="">
     <input type="checkbox" name="categorySelect[]" id="1" onclick="categoryOnClick();"/>
</form>

回答by

Working jsFiddle Demo

工作 jsFiddle 演示

function categoryOnClick() {
    var rows = document.getElementsByName('categorySelect[]');
    var selectedRows = [];
    for (var i = 0, l = rows.length; i < l; i++) {
        if (rows[i].checked) {
            selectedRows.push(rows[i]);
        }
    }

    alert(selectedRows.length);
}

回答by Rachit Doshi

In javascript you can do

在javascript中你可以做

function categoryOnClick() {
    var count = document.querySelectorAll("input[type=checkbox]").length;
    var checkedElements = 0;
    for (var i = 0; i < count; i++) {
        checkedElements = checkedElements + parseInt((document.querySelectorAll("input[type=checkbox]")[i].checked) ? 1 : 0);    
    }
    alert(checkedElements);
}

and in jquery :

在 jquery 中:

function categoryOnClick() {           
    alert($('input:checked').length)
}

Hope that helps

希望有帮助

回答by derHofbauer

You can do this using jQuery:

您可以使用 jQuery 执行此操作:

function categoryOnClickjQuery() {
    var selectedRows = $('form input').attr('name', 'categorySelect[]');
    alert(selectedRows.length);
}