我有多个jQuery复选框,可以通过name属性获取选中的复选框值。我的问题是如何使复选框删除以前检查的值并显示新值。
在目前的情况下,它正在加入价值,而不是相互改变。
此处示例HTML代码:
<div class="drop-column">
<label class="column-label"><input class="column-checkbox" type = "checkbox" rel = "All
Column" name = "All Filter" />Filter (All Fields)</label>
<label class="column-label"><input class="column-checkbox" type = "checkbox" rel =
"Name" name="Name" />Name</label>
<label class="column-label"><input class="column-checkbox" type = "checkbox" rel =
"Department" name = "Department" />Department</label>
<label class="column-label"><input class="column-checkbox" type = "checkbox" rel =
"Manager" name = "Manager" />Manager</label>
</div>
<div class="filter-by-col-text"></div>
和jQuery for do the function
$('div.drop-column').delegate('.column-checkbox', 'change', function () {
var $checked = $('.column-checkbox:checked');
var rels = [];
$checked.each(function () {
rels.push($(this).attr('name'));
});
$('.filter-by-col-text').html(rels);
});
链接到小提琴测试http://jsfiddle.net/nucleo1985/f6t9M/
谢谢,
答案 0 :(得分:0)
使用:
$('div.drop-column').delegate('.column-checkbox', 'change', function () {
$('input:checkbox').removeAttr('checked');
$(this).prop('checked','checked');
var $checked = $('.column-checkbox:checked');
var rels;
$checked.each(function () {
rels=($(this).attr('name'));
});
$('.filter-by-col-text').html(rels);
});
<强> Demo 强>