我有两个复选框(见下文),当点击时,激活一个范围的背景颜色的变化。问题是,我需要它使得这些复选框中只有一个可以被激活,而另一个被取消激活。我知道所有关于单选按钮的东西,问题是即使单选按钮被停用,它也不会删除跨度的背景色。我需要一个解决方案,只允许一个被选择,并自动删除背景颜色时,另一个选项被选择。
示例:http://eblacksmith.com/test/
核心代码:
$(document).ready(function() {
$("span[class='checkbox']").addClass("unchecked");
$(".checkbox").click(function(){
if($(this).children("input").attr("checked")){
// uncheck
$(this).children("input").attr("checked", false);
$(this).removeClass("checked");
$(this).addClass("unchecked");
}else{
// check
$(this).children("input").attr("checked", true);
$(this).removeClass("unchecked");
$(this).addClass("checked");
}
//alert($(this).children("input").attr("checked"));
});
});发布于 2013-03-14 00:20:36
下面的方法应该是可行的
$(document).ready(function() {
// get a list of all the checkboxes
var $checkboxEls = $('input[type="checkbox"]');
$(document).on('change', 'input[type="checkbox"]', function(event) {
// highlight this check box
$(this).parent('span').addClass('checked');
// remove other classes
$checkboxEls.not(this).removeAttr('checked').parent('span').removeClass('checked');
});
});它使用not()从我们之前存储的复选框列表中删除当前的复选框。Example here
https://stackoverflow.com/questions/15390291
复制相似问题