jquery - How to count checked checkbox with Javascript? - Stack Overflow

I would like to count the checkboxes that are checked and display the count in the Div.Here is my HTML

I would like to count the checkboxes that are checked and display the count in the Div.

Here is my HTML :

<form name="liste_figurine" method="post">

<input type="checkbox" id="radio-1" class="z" name="chck1[]" onclick="updateCount()" />
<input type="checkbox" id="radio-2" class="z" name="chck2[]" onclick="updateCount()" />

</form>

<div id="y"></div>

Here is my JS :

function updateCount {
    var x = $(".z:checked").length;
    document.getElementById("y").innerHTML = x;
};

Here is an exemple : /

Sorry, I'm not really used to JS... What is wrong with my code ?


Finally, it's working but I have one last issue : the onclick must contain 2 functions and I don't manage to make them working together.

Now this function works, alone : onclick="updateCount()"

This other function works also, alone : onclick="document.getElementById(\'radio-'.$ligne['id'].'-2\‌​').c‌​hecked = false"

But these two functions doesn't work, together : onclick="updateCount(); fx2(document.getElementById(\'radio-'.$ligne['id'].'-1\').ch‌​ecked = false);"

What is wrong with the third proposition ? Is it a syntax error ?

I would like to count the checkboxes that are checked and display the count in the Div.

Here is my HTML :

<form name="liste_figurine" method="post">

<input type="checkbox" id="radio-1" class="z" name="chck1[]" onclick="updateCount()" />
<input type="checkbox" id="radio-2" class="z" name="chck2[]" onclick="updateCount()" />

</form>

<div id="y"></div>

Here is my JS :

function updateCount {
    var x = $(".z:checked").length;
    document.getElementById("y").innerHTML = x;
};

Here is an exemple : https://jsfiddle/r3todbs6/3/

Sorry, I'm not really used to JS... What is wrong with my code ?


Finally, it's working but I have one last issue : the onclick must contain 2 functions and I don't manage to make them working together.

Now this function works, alone : onclick="updateCount()"

This other function works also, alone : onclick="document.getElementById(\'radio-'.$ligne['id'].'-2\‌​').c‌​hecked = false"

But these two functions doesn't work, together : onclick="updateCount(); fx2(document.getElementById(\'radio-'.$ligne['id'].'-1\').ch‌​ecked = false);"

What is wrong with the third proposition ? Is it a syntax error ?

Share Improve this question edited Mar 30, 2017 at 2:42 Guillaume asked Mar 29, 2017 at 23:58 GuillaumeGuillaume 3521 gold badge9 silver badges24 bronze badges 9
  • 2 .size() should be .length – Barmar Commented Mar 29, 2017 at 23:59
  • 1 .length is not .length() – A. L Commented Mar 30, 2017 at 0:03
  • You may use .forEach(value => { }) to check value of checkbox and count it s amount under condition – Jakub Chlebowicz Commented Mar 30, 2017 at 0:03
  • OK, I corrected this. By the way, do I have the obligation to use jquery, as I see you edited my post ? – Guillaume Commented Mar 30, 2017 at 0:03
  • 1 The pure JavaScript way is document.querySelectorAll(".z:checked").length – castletheperson Commented Mar 30, 2017 at 0:07
 |  Show 4 more ments

5 Answers 5

Reset to default 2
window.updateCount = function() {
    var x = $(".z:checked").length;
    document.getElementById("y").innerHTML = x;
};

https://jsfiddle/73yfczcj/

try this. you have to use jquery in javascript setting.

Use length

function updateCount(){
    var x = $("input:checked").length;
    $("#y").html(x);
};
function updateCount {
    var x = document.querySelector('.z:checked').length;
    document.getElementById("y").innerHTML = x;
};

try this fuction if you don't want to use jQuery

There are three issues for your Fiddle code:

  • As others have mentioned, it should be length, not size().
  • You should change the JavaScript setting -> Load type to "No wrap - in <head>". Otherwise, updateCount is not defined in global scope.
  • You need to configure Fiddle to use jQuery if you use your original code.

A working example: https://jsfiddle/c5ez4w7y/

The answer provided by kangsu is the best answer but here is how I did it.

var form = document.getElementById('form');
var checkBoxes = $(form).children('.checkbox');
var count = 0;

var divBoxesChecked = document.getElementById('boxesChecked');
divBoxesChecked.innerHTML = 0;

$(checkBoxes).on('click', function() {
  $.each(checkBoxes, function(i) {
    if (checkBoxes[i].checked) {
      count++;
    }
  });
  console.log(count);
  divBoxesChecked.innerHTML = count;
  count = 0;
});
<script src="https://code.jquery./jquery-3.2.1.min.js"></script>
<form id="form" method="post">

  <input type="checkbox" id="box_01" class="checkbox" name="box_01" />
  <label>Box 1</label>

  <input type="checkbox" id="box_02" class="checkbox" name="box_02" />
  <label>Box 2</label>

  <input type="checkbox" id="box_03" class="checkbox" name="box_03" />
  <label>Box 3</label>

</form>

<div id="boxesChecked"></div>

Basically, every time one of the boxes is clicked it counts how many boxes are checked in total and prints that out to the div and the console, then it resets the counter to zero since it re-counts all of the checked boxes every time you click on one.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745469341a4629061.html

相关推荐

  • jquery - How to count checked checkbox with Javascript? - Stack Overflow

    I would like to count the checkboxes that are checked and display the count in the Div.Here is my HTML

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信