简体   繁体   English

使用 Javascript 计算表单上的复选框

[英]Counting checkboxes on a form using Javascript

I'm working on a form on a bespoke website (it's abit like a CMS just to explain the formatting of the HTML below so alot of it isn't relevant) and one of the things I've been requested to implement is to count how many of the checkboxes on the form have been ticked.我正在一个定制网站上制作一个表格(它有点像 CMS,只是为了解释下面 HTML 的格式,所以很多都不相关),我被要求实施的一件事是计算表格上的复选框有多少已被勾选。

I've got checkbox1, checkbox2 and checkbox3 and a field called numberticked which I'm going to hide when it gets submitted (which I know how to do).我有 checkbox1、checkbox2 和 checkbox3 以及一个名为 numberticked 的字段,当它被提交时我将隐藏它(我知道该怎么做)。 If the user ticked checkbox1 it would write 1 to that numberticked field, if they ticked all 3 it would write 3 to that field and so on and so on (I'll be adding more checkboxes but I thought I'd start with 3 to test.)如果用户勾选了 checkbox1,它将向该 numberticked 字段写入 1,如果他们勾选所有 3,它将向该字段写入 3,依此类推(我将添加更多复选框,但我认为我会从 3 开始测试。)

I've had a read on the site but I'm not sure how to start on this one since it's a bespoke site, but it uses labels and ids and the form does work with Javascript so I'm hoping there's a general way to write a script to do it!我已经在该网站上阅读过,但我不知道如何从这个网站开始,因为它是一个定制网站,但它使用标签和 ID,并且该表单确实适用于 Javascript 所以我希望有一种通用的方法写一个脚本来做!

<div class="seq-box-form-field  editorcheckbox1  ">
  <label for="checkbox1">
    <span style="padding-right: 10px; vertical-align: 1px;">Checkbox 1</span>
    <input type="checkbox" name="checkboxcheckbox1" id="checkboxcheckbox1" />
    <input type="hidden" name="checkbox1" id="checkbox1" value="false" />
  </label>
  <script>
    $(document).ready(function() {
      $('#checkboxcheckbox1').click(function() {
        if ($('#checkboxcheckbox1').val() == 'true') {
          $('#checkbox1').val('false');
          $('#checkboxcheckbox1').val('false');
          $('#checkboxcheckbox1').prop('checked', false);
          $('#aspire_previewrefresh').trigger('click');
        } else {
          $('#checkbox1').val('true');
          $('#checkboxcheckbox1').val('true');
          $('#checkboxcheckbox1').prop('checked', true);
          $('#aspire_previewrefresh').trigger('click');
        };
      });
    });
  </script>
</div>
<div class="seq-box-form-field  editorcheckbox2  ">
  <label for="checkbox2">
    <span style="padding-right: 10px; vertical-align: 1px;">Checkbox 2</span>
    <input type="checkbox" name="checkboxcheckbox2" id="checkboxcheckbox2" />
    <input type="hidden" name="checkbox2" id="checkbox2" value="false" />
  </label>
  <script>
    $(document).ready(function() {
      $('#checkboxcheckbox2').click(function() {
        if ($('#checkboxcheckbox2').val() == 'true') {
          $('#checkbox2').val('false');
          $('#checkboxcheckbox2').val('false');
          $('#checkboxcheckbox2').prop('checked', false);
          $('#aspire_previewrefresh').trigger('click');
        } else {
          $('#checkbox2').val('true');
          $('#checkboxcheckbox2').val('true');
          $('#checkboxcheckbox2').prop('checked', true);
          $('#aspire_previewrefresh').trigger('click');
        };
      });
    });
  </script>
</div>
<div class="seq-box-form-field  editorcheckbox3  ">
  <label for="checkbox3">
    <span style="padding-right: 10px; vertical-align: 1px;">Checkbox 3</span>
    <input type="checkbox" name="checkboxcheckbox3" id="checkboxcheckbox3" />
    <input type="hidden" name="checkbox3" id="checkbox3" value="false" />
  </label>
  <script>
    $(document).ready(function() {
      $('#checkboxcheckbox3').click(function() {
        if ($('#checkboxcheckbox3').val() == 'true') {
          $('#checkbox3').val('false');
          $('#checkboxcheckbox3').val('false');
          $('#checkboxcheckbox3').prop('checked', false);
          $('#aspire_previewrefresh').trigger('click');
        } else {
          $('#checkbox3').val('true');
          $('#checkboxcheckbox3').val('true');
          $('#checkboxcheckbox3').prop('checked', true);
          $('#aspire_previewrefresh').trigger('click');
        };
      });
    });
  </script>
</div>
<div class="seq-box-form-field editornumberticked  ">
  <label for="numberticked">Number Ticked</label>
  <input id="numberticked" name="numberticked" placeholder="" type="text" onkeydown="if (event.keyCode == 13) { event.preventDefault(); return false; }" value="" />

Here is an example of how to keep a text/hidden field's value updated to match the number of checkboxes currently checked.这是一个示例,说明如何更新文本/隐藏字段的值以匹配当前选中的复选框的数量。

 $(() => { $('input:checkbox').change(() => { $('#count').val($('input:checkbox:checked').length); }); });
 <:DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Example</title> <script src="https.//code.jquery.com/jquery-3.1.0.js"></script> </head> <body> <input type="checkbox" /> A<br/> <input type="checkbox" /> B<br/> <input type="checkbox" /> C<br/> <input type="text" id="count" value="0" /> </body> </html>

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

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