我已经找到了关于检查组中复选框的一些答案,但即使我将其复制到我的方面它也不会起作用。 我有一个简单的复选框组示例。 任何人都可以解释一下我做错了什么吗?
var options = [];
$('.dropdown-menu a').on('click', function(event) {
var $target = $(event.currentTarget),
val = $target.attr('data-value'),
$inp = $target.find('input'),
idx;
if ((idx = options.indexOf(val)) > -1) {
options.splice(idx, 1);
setTimeout(function() {
$inp.prop('checked', false)
}, 0);
} else {
options.push(val);
setTimeout(function() {
$inp.prop('checked', true)
}, 0);
}
$(event.target).blur();
console.log(options);
return false;
});
$("#checkAll").click(function() {
$(".check").prop('checked', $(this).prop('checked'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<div class="btn-group btn-group-sm">
<input id="btnZatwierdz" class="btn btn-default btn-sm dropdown-toggle" type="submit" value="Filtruj" aria-haspopup="true" aria-expanded="false" onserverclick="btnZatwierdzClick" runat="server"></input>
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-cog"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="#" class="small" data-value="option1" tabindex="-1">
<input id="CheckboxHunting" type="checkbox" runat="server" />
Hunting
</a>
</li>
<li>
<a href="#" class="small" data-value="option2" tabindex="-1">
<input id="CheckboxFarming" type="checkbox" runat="server" />
Farming
</a>
</li>
<li class="dropdown-submenu">
<a href="#" class="small" data-value="option1" tabindex="-1">
<input id="checkAll" type="checkbox" />
Hunting
</a>
<ul class="dropdown-menu">
<li>
<a href="#" class="check" data-value="option1" tabindex="-1">
<input id="Checkbox2" name="checkbox-2" type="checkbox" runat="server" />
Hunting
</a>
</li>
<li>
<a href="#" class="check" data-value="option2" tabindex="-1">
<input id="Checkbox3" name="checkbox-3" type="checkbox" runat="server" />
Farming
</a>
</li>
</ul>
</li>
</ul>
</div>
答案 0 :(得分:0)
试试这个
$("#checkAll").click(function() {
$(this).closest("li").find("input[id^='Checkbox']").prop('checked', $(this).prop('checked'));
});
这将取最接近的li
$("#checkAll")
,然后查找以inputs
开头的所有Checkbox
(id^='Checkbox'
)
var options = [];
$('.dropdown-menu a').on('click', function(event) {
var $target = $(event.currentTarget),
val = $target.attr('data-value'),
$inp = $target.find('input'),
idx;
if ((idx = options.indexOf(val)) > -1) {
options.splice(idx, 1);
setTimeout(function() {
$inp.prop('checked', false)
}, 0);
} else {
options.push(val);
setTimeout(function() {
$inp.prop('checked', true)
}, 0);
}
$(event.target).blur();
console.log(options);
return false;
});
$("#checkAll").click(function() {
$(this).closest("li").find("input[id^='Checkbox']").prop('checked', $(this).prop('checked'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<div class="btn-group btn-group-sm">
<input id="btnZatwierdz" class="btn btn-default btn-sm dropdown-toggle" type="submit" value="Filtruj" aria-haspopup="true" aria-expanded="false" onserverclick="btnZatwierdzClick" runat="server"></input>
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-cog"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
<a href="#" class="small" data-value="option1" tabindex="-1">
<input id="CheckboxHunting" type="checkbox" runat="server" /> Hunting
</a>
</li>
<li>
<a href="#" class="small" data-value="option2" tabindex="-1">
<input id="CheckboxFarming" type="checkbox" runat="server" /> Farming
</a>
</li>
<li class="dropdown-submenu">
<a href="#" class="small" data-value="option1" tabindex="-1">
<input id="checkAll" type="checkbox" /> Hunting
</a>
<ul class="dropdown-menu">
<li>
<a href="#" class="check" data-value="option1" tabindex="-1">
<input id="Checkbox2" name="checkbox-2" type="checkbox" runat="server" /> Hunting
</a>
</li>
<li>
<a href="#" class="check" data-value="option2" tabindex="-1">
<input id="Checkbox3" name="checkbox-3" type="checkbox" runat="server" /> Farming
</a>
</li>
</ul>
</li>
</ul>
</div>