Javascript 计数选中复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11838667/
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-08-24 07:18:48  来源:igfitidea点击:

Javascript count checked checkbox

javascriptcheckboxchecked

提问by user1148875

I know that may have a few similar questions @stackoverflow, but I didnt found any solution to my problem yet :s

我知道@stackoverflow 可能有一些类似的问题,但我还没有找到任何解决我的问题的方法:s

<?php
while($rowVideo = mysql_fetch_array($ResultQueryVideo))
{
?>
<input type="checkbox" name = "checkbox-1[]" class="checkbox" value ="<?php echo $rowVideo['idVideo'] ?>" /> <?php....some code...

This results a few checkbox's, the same number as idVideo..thats the point.

这会产生一些复选框,与 idVideo 的数字相同。这就是重点。

Now, before the submit, I need to be sure that at least one checkbox is checked. But i was not sucesseful :x

现在,在提交之前,我需要确保至少选中了一个复选框。但我没有成功:x

function isCountCheck (helperMsg) {
    var chkCnt = 0;
    var Frm = document.forms[0];
    var dLen = Frm.length - 1;
    for (i=0;i<=dLen;i++) {
        if(document.form1.["checkbox-1[]"].checked) chkCnt++;
    }

    if (chkCnt>0) {
        return true;
    } else {
        alert(helperMsg);
        return false;
    }
}

Extra details: form name = "form1"

额外细节:表单名称 = "form1"

Can you guide me a litle ? Thanks

你能指导我一下吗?谢谢

EDIT:

编辑:

function isCountCheck(){
    if($("input[type=checkbox]:checked").length > 0)
    {
    return true;
    }
    else
    {
    alert('Invalid');
    return false;
    }
}

But still not working..even that alert is shown..

但仍然无法正常工作..即使显示警报..

采纳答案by nnnnnn

The main problem is that you're not using the iindex within the loop to reference the individual checkboxes, and you've got a .just before the [which is a syntax error. So change:

主要问题是您没有使用i循环中的索引来引用各个复选框,并且您在.之前[有一个语法错误。所以改变:

if(document.form1.["checkbox-1[]"].checked) chkCnt++;

To:

到:

if(document.form1["checkbox-1[]"][i].checked) chkCnt++;

But you can neaten up the function somewhat as follows:

但是您可以按如下方式整理该功能:

function isCountCheck(helperMsg) {
    var i, dLen = document.form1["checkbox-1[]"].length;
    // if the length property is undefined there is only one checkbox
    if (typeof dLen === "undefined") {
        if (document.form1["checkbox-1[]"].checked) return true;
    }
    else {
        for (i = 0; i < dLen; i++) {
            if (document.form1["checkbox-1[]"][i].checked) return true;
        }
    }
    alert(helperMsg);
    return false;
}

Demo: http://jsfiddle.net/nnnnnn/ZjK3w/1/

演示:http: //jsfiddle.net/nnnnnn/ZjK3w/1/

Or just loop through allinputs in the form, checking the type (and/or the name) of each one:

或者只是遍历表单中的所有输入,检查每个输入的类型(和/或名称):

function isCountCheck(helperMsg) {
    var i, len, inputs = document.form1.getElementsByTagName("input");
    for (i = 0, len = inputs.length; i < len; i++) {
        if (inputs[i].type === "checkbox"
            && inputs[i].checked)
            return true;
    }
    alert(helperMsg);
    return false;
}

Demo: http://jsfiddle.net/nnnnnn/ZjK3w/2/

演示:http: //jsfiddle.net/nnnnnn/ZjK3w/2/

回答by vsync

Simplest solution:

最简单的解决方案:

var form = document.forms[0]; // your form element (whatever)
var checkedElms = form.querySelectorAll(':checked').length;

No need for jQuery. Supported down to IE8. use polyfill for older browsers if you wish.

不需要 jQuery。支持到 IE8。如果您愿意,可以为旧版浏览器使用 polyfill。

回答by N. Taylor Mullen

Use Jquery:

使用jQuery:

function isCountCheck(helperMsg){
    if($("input[type=checkbox]:checked").length > 0)
        return true;
    alert(helperMsg);
    return false;
}