简体   繁体   English

编写jQuery代码以检查是否选中了复选框,并指出选中的复选框数量?

[英]Write jquery code to check if the check boxes are checked or not and indicate the number of check boxes checked?

  $(document).ready(function(){ var numberOfChecked = $('input:checkbox:checked').length; //alert(numberOfChecked); // document.write(numberOfChecked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td><input type="checkbox"/><lable>Red</lable></td> <td><input type="checkbox"/><lable>Green</lable></td> <td><input type="checkbox"/><lable>Blue</lable></td> <td><input type="checkbox"/><lable>Black</lable></td> </tr> </table> <p id="dem"></p> 

This is actually an assignment oi was stuck with the dynamically changing number as im new to jquery. 这实际上是oi随动态变化的数字卡住的分配,这是jquery的新功能。

Your problem was that you were updating the count as soon as the page loads (which happens before you get a chance to check any boxes and then doesn't happen when you do check a box), not after each checkbox gets clicked. 您的问题是,您要在页面加载后立即更新计数(这是在您有机会选中任何复选框之前发生的,然后在您选中复选框时不会发生),而不是在单击每个复选框之后才进行更新。

You asked for a JQuery solution to this problem, but understand that JQuery is just JavaScript that someone else wrote. 您要求针对此问题的JQuery解决方案,但要了解JQuery只是其他人编写的JavaScript。 For a trivial issue like this, adding the JQuery library really adds no value - - the problem is easily solved with straight JavaScript. 对于像这样的小问题,添加JQuery库确实没有任何价值-使用简单的JavaScript可以轻松解决该问题。 JQuery is recklessly overused today. JQuery今天被滥用了。 When it came out, it was much simpler than doing things without it, but that was quite a while ago and today, we can do much of the basics of JQuery just as simply without it. 当它问世时,它比没有它做事要简单得多,但是在很久以前和今天,我们可以像没有它一样简单地完成JQuery的许多基础知识。

 // Get all the checkboxes into an array var boxes = Array.prototype.slice.call(document.querySelectorAll("input[type=checkbox]")); // Get reference to output span var output = document.getElementById("output"); // Loop through all the checkboxes boxes.forEach(function(box){ // Set up click event handlers on each box.addEventListener("change", function(){ // Update the output area with the number of checked checkboxes output.textContent = document.querySelectorAll("input[type=checkbox]:checked").length; }); }); 
 <input type="checkbox"> Box 1 <input type="checkbox"> Box 2 <input type="checkbox"> Box 3 <input type="checkbox"> Box 4 <div>Number checked: <span id="output"></span></div> 

If you feel you really want a JQuery solution, here's what that would look like: 如果您真的想要一个JQuery解决方案,则如下所示:

 $(function(){ $("input[type=checkbox]").on("change", function(){ var numberOfChecked = $('input:checkbox:checked').length; $("#dem").text(numberOfChecked); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td><input type="checkbox"/><lable>Red</lable></td> <td><input type="checkbox"/><lable>Green</lable></td> <td><input type="checkbox"/><lable>Blue</lable></td> <td><input type="checkbox"/><lable>Black</lable></td> </tr> </table> <p id="dem"></p> 

$(":checkbox :checked") 

this gets the checked checkboxes in an array if you need the length of checked you can do $(":checkbox :checked").length 这将获得数组中选中的复选框,如果您需要选中的长度,则可以执行$(":checkbox :checked").length

if you just do $(":checked") you get selections as well for drop down lists etc. 如果您只执行$(“:checked”),那么您还将获得下拉列表等的选择。

so you seem to have the checkbox part; 因此,您似乎具有复选框部分; what needs to be done is you need to trigger an action; 需要做的是您需要触发一个动作; where you threw the code in the javascript file runs only once. 您在javascript文件中投放代码的位置仅运行一次。 triggers are called when an event happens. 事件发生时调用触发器。 http://api.jquery.com/trigger/ http://api.jquery.com/trigger/

since we are going by when something is checked (unless you want to tie this to a button ofc) you would want to call it on a change 因为我们正在检查某项内容(除非您希望将此内容绑定到按钮ofc),所以您希望在更改时调用它

$(".checkbox").change(function() {
   $("#areaforInput").append($(":checkbox:checked").length)
});

with the areaForInput being the id for the object you want to append with the count for checked etc 其中areaForInput是要附加的对象的ID,并带有选中的计数

<input type="checkbox"> Box 1
<input type="checkbox"> Box 2
<input type="checkbox"> Box 3
<input type="checkbox"> Box 4
<div>Number checked: <span id="output"></span></div>

JS: JS:

$(document).ready(function(){
  $("input[type=checkbox]").change(function(){
      $("#output").text($("input[type=checkbox]:checked").length);
   });
});

fiddle updated https://fiddle.jshell.net/riazxrazor/koo23g47/3/ 小提琴更新了https://fiddle.jshell.net/riazxrazor/koo23g47/3/

Js s

<script>
$(function () {
    $(".chkBox").click(function () {
        $("#output").html($(".chkBox:checked").length)
    });
});
</script>

Html HTML

<input class="chkBox" type="checkbox" />Red
<input class="chkBox" type="checkbox" />Green
<input class="chkBox" type="checkbox" />Blue
<input class="chkBox" type="checkbox" />Black
<div>Number checked: <span id="output"></span></div>

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

相关问题 使用JavaScript检查和控制已选中复选框的数量 - Check and control the number of checked check boxes with JavaScript 限制允许选中的复选框数量 - Limit the number of check-boxes allowed to be checked 选中了多少个复选框 - How many check boxes are checked 在检查“未检查”后,如何强制Jquery最后检查“检查”框? - How to force Jquery to check for CHECKED boxes last, after checking for NOT CHECKED? 复选框:当有多个选中的复选框时,启用/禁用未选中的复选框 - Check box: Enable/Disable unchecked boxes when there is a number of checked ones 根据选中的复选框数更改表单的文本字段 - Change text field of form based on number of check boxes checked 根据使用CSS或JS选中的复选框数隐藏div - Hide divs based on number of check boxes checked with CSS or JS Jquery:选中“全选”时选择所有复选框 - Jquery: Select all check boxes when 'select all' is checked jQuery clickable未选中输入:复选框? 选择所有复选框? - jQuery clickable not checked input:checkbox? select all check boxes? 计算多个面板中选中的复选框的数量,并使用 jQuery/JavaScript 在正确的面板中显示相关数字 - Count the number of checked check-boxes in multiple panels and show the relevant number in the correct panel using jQuery/JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM