[英]Count how many times a single checkbox is checked
How can I count how many times a checkbox is toggled from unchecked to checked?如何计算复选框从未选中切换到选中的次数?
<input type="checkbox" id="auto_<?= $sub_module_id ?>" onclick="test(<?= $sub_module_id ?>)" />
My JavaScript code:我的 JavaScript 代码:
function test(sub_module_id){
$('#auto_'+sub_module_id).is(':checked'); //returns true,
// I want to count when checked
}
Instead of having a variable assigned with the count of how many times something is checked off why don't you assign the count to a data property since the count is specifically in relation to the element?与其给变量分配一个被检查的次数的计数,为什么不将计数分配给数据属性,因为计数是专门与元素相关的?
$(":checkbox").click(function () {
var $this = $(this);
if (typeof ($this.data("count")) == "undefined") {
$this.data("count", 0);
};
if ($this.is(":checked")) {
$this.data("count", $this.data("count") + 1)
alert($this.data("count"));
}
});
Then when you need to access it you can use $("#check1").data("count");
然后当你需要访问它时,你可以使用
$("#check1").data("count");
http://jsfiddle.net/SeanWessell/xsazxevk/ http://jsfiddle.net/SeanWesell/xsazxevk/
You can use the function below to count the number of times a single checkbox has been checked:您可以使用下面的函数来计算单个复选框被选中的次数:
var countChk = 0;
function test(){
if(jQuery('#auto').is(':checked'))
{
countChk = countChk+1;
alert(countChk);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.