简体   繁体   English

使用javascript计数表单中的复选框

[英]count checkboxes in a form using javascript

I have quite a lot of check boxes on one form. 我在一种表单上有很多复选框。 The check boxes are in different sections of the form. 复选框位于表单的不同部分。 I would like to count the number of checkboxes at the end of each section on my form. 我想计算表单每一部分末尾的复选框数量。

For example I have 6 sections within my form and I have between 6 and 10 checkboxes within each section. 例如,我的表单中有6个部分,每个部分中有6到10个复选框。 I would like to have a textbox with a number value at the end of each section telling me how many check boxes were check within that particular section. 我想在每个部分的末尾都有一个带有数字值的文本框,告诉我在该特定部分中选中了多少个复选框。

Does anyone have a script for that? 有人为此写脚本吗? I have a snippet from support staff but they don't have a full solution and I don't know JavaScript well enough to finish it. 我有支持人员的摘录,但他们没有完整的解决方案,而且我对JavaScript的了解还不够。 I'm through trying to figure it out so i can finish it. 我正在努力弄清楚,以便我可以完成它。 Here is the snippet they sent me: 这是他们发送给我的摘录:

<script type="text/JavaScript">
function countcheck(checkName){

    inputElems = document.getElementsByName(checkName);
    count = 0;
    for (i = 0; i < inputElems.length; i++) {
        if (inputElems.checked === true) {
            count++;
            document.getElementById("teval_engage7").value = count;
        }
    }
}
</script>   

The script will only count checked checkboxes within that group only. 该脚本仅会计数该组中的选中复选框。 Basically you will need a function for each of your checkbox so that you can have separated counters. 基本上,每个复选框都需要一个功能,以便可以有独立的计数器。 This will also require an attribute to your checkbox according to the function in question: 根据相关功能,这还将要求您的复选框具有一个属性:

onclick="countcheck(this.name);"
var cb_counts = {};
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
    var input = inputs[i];
    if (input.type = 'checkbox' && input.checked) {
        if (cb_counts[input.name]) {
            cb_counts[input.name]++;
        } else {
            (cb_counts[input.name] = 1);
        }
    }
}

Now the object cb_counts contains properties with the name of each group of checkboxes, and the values are the counts. 现在,对象cb_counts包含具有每组复选框名称的属性,并且值是计数。 Do with this what you wish. 随心所欲地做到这一点。

Thanks for the quick reply. 感谢您的快速回复。 I use a application call rsform which helps to make forms. 我使用一个有助于生成表格的应用程序rsform。 On the script I have "teval_engage7" is the text box which stores the value of the number of checkboxes that have been checked. 在脚本中,我有“ teval_engage7”是一个文本框,用于存储已选中的复选框数的值。 "onclick="countcheck(this.name);"" is the trigger I place under each checkbox question. “ onclick =“ countcheck(this.name);”“是我放置在每个复选框问题下的触发器。 So when I go to the form and click on a checkbox with that trigger attached to it, the value of "1" shows up in the teval_engage7 text box. 因此,当我进入表单并单击带有该触发器的复选框时,在teval_engage7文本框中将显示“ 1”的值。 The next check box I click on then shows "2" in the teval_engage7 text box. 我单击的下一个复选框,然后在teval_engage7文本框中显示“ 2”。 My question is, can you te me using this script you wrote where the values are stored so I can substitute that name for my textbox name. 我的问题是,您可以使用编写的脚本将值存储在哪里,以便将其替换为我的文本框名称。 Also, do I use my same trigger "onclick="countcheck(this.name);"" to attach to my checkbox attibute area to trigger the count? 另外,我是否使用相同的触发器“ onclick =” countcheck(this.name);“”附加到复选框外观区域来触发计数?

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM