[英]Add toggleClass to a Checkbox with jQuery
How can I apply toggleClass() to an input="checkbox" when it has been checked? 选中后,如何将toggleClass()应用于input =“ checkbox”? I'm trying to add the class "calcButtonOn" when checked:
我试图在检查时添加类“ calcButtonOn”:
jQuery(function() {
var total = 0;
jQuery("input").click(function() {
total = 0;
jQuery("input:checked").not('input[value="reset"]').each(function() {
total += parseFloat(jQuery(this).val());
});
jQuery("#Totalcost").html("Save up to " + total + "%");
});
});
Select all the checkboxes with JQuery and add an event handler on change
event: 选中所有带有JQuery的复选框,并在
change
事件上添加一个事件处理程序:
jQuery("input[type=checkbox]").change(function(){
jQuery(this).toggleClass("calcButtonOn", this.checked);
});
Use : 采用 :
$(".Your_Checkbox_Class")each(function(){
if ($(this).is(':checked') ){
$(this).toggleClass("Your_New_Class");
}
});
Explanation : Loop thru all Checkboxes (hope you have given them Class as i gave above ).And then check if its checked , if yes toggle the class. 说明:循环遍历所有Checkboxes(希望您已经给了我上面给出的Class类)。然后检查是否选中了,如果是,则切换类。
Edit 编辑
Well your question is not 100% clear. 好吧,您的问题不是100%清楚的。 But if you want event on click of only one checkbox and toggle only that checkbox , then use
但是,如果您希望仅单击一个复选框并触发该事件,则使用
$(".Your_Checkbox_Class").click(function(){
var id = $(this).attr("id");
$("#"+id).toggleClass("Your_New_Class");
})
Explanation : If checkbox gets checked , then toggle class. 说明:如果checkbox被选中,则切换类。 I assume you have proper classes on DOM load .
我假设您在DOM load上有适当的类。 Like if after Loading DOM , if a checkbox is already ticked (Came from server side), it will already have class for
checked_element
already. 就像在加载DOM之后一样,如果已经选中了一个复选框(从服务器端来),则该复选框将已经具有
checked_element
类。
I'd suggest, on the assumption that you want to have the class present when the element is checked, and removed when the element is subsequently unchecked: 我建议,假设您希望在检查元素时让类存在,而在随后取消检查元素时将其删除:
jQuery('input[type=checkbox]').on('change', function () {
$(this).toggleClass('active', this.checked);
});
jQuery('input[type=checkbox]').on('change', function () { $(this).toggleClass('active', this.checked); });
input { display: block; padding: 0.5em; margin: 0 0 1em 0; box-shadow: none; } input.active { box-shadow: 0 0 1em 0.25em #f00; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" />
Should any of your check-boxes be checked on page-load (via the checked
attribute, for example) then it's worth further chaining the change()
method (without a function callback) in order to fire the event: 如果在页面加载时选中了您的任何复选框(例如,通过
checked
属性),则值得进一步链接change()
方法(不带函数回调)以触发事件:
jQuery('input[type=checkbox]').on('change', function () {
$(this).toggleClass('active', this.checked);
}).change();
jQuery('input[type=checkbox]').on('change', function () { $(this).toggleClass('active', this.checked); }).change();
input { display: block; padding: 0.5em; margin: 0 0 1em 0; box-shadow: none; } input.active { box-shadow: 0 0 1em 0.25em #f00; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" checked /> <input type="checkbox" /> <input type="checkbox" checked /> <input type="checkbox" />
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.