admin管理员组文章数量:1426066
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\').checked = false"
But these two functions doesn't work, together :
onclick="updateCount(); fx2(document.getElementById(\'radio-'.$ligne['id'].'-1\').checked = 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\').checked = false"
But these two functions doesn't work, together :
onclick="updateCount(); fx2(document.getElementById(\'radio-'.$ligne['id'].'-1\').checked = 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
5 Answers
Reset to default 2window.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
, notsize()
. - 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.
本文标签: jqueryHow to count checked checkbox with JavascriptStack Overflow
版权声明:本文标题:jquery - How to count checked checkbox with Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745469341a2659677.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论