我有以下几点:
$(document).ready(function()
{
$("#select-all-teammembers").click(function() {
$("input[name=recipients\\[\\]]").attr('checked', true);
});
});
我希望id="select-all-teammembers"在选中和未选中之间切换。想法吗?那不是几十行代码吗?
我有以下几点:
$(document).ready(function()
{
$("#select-all-teammembers").click(function() {
$("input[name=recipients\\[\\]]").attr('checked', true);
});
});
我希望id="select-all-teammembers"在选中和未选中之间切换。想法吗?那不是几十行代码吗?
当前回答
你也可以这样写
$(function() {
$("#checkbox-toggle").click(function() {
$('input[type=checkbox][name=checkbox_id\\[\\]]').click();
});
});
当用户单击id为“#checkbox-toggle”的按钮时,只需要调用复选框的单击事件。
其他回答
你可以这样写:
$(document).ready(function() {
$("#select-all-teammembers").click(function() {
var checkBoxes = $("input[name=recipients\\[\\]]");
checkBoxes.prop("checked", !checkBoxes.prop("checked"));
});
});
在jQuery 1.6之前,当我们只有attr()而没有prop()时,我们习惯这样写:
checkBoxes.attr("checked", !checkBoxes.attr("checked"));
但是prop()在应用于“布尔”HTML属性时具有比attr()更好的语义,所以在这种情况下通常是首选。
从jQuery 1.6开始,你可以使用.prop(function)来切换每个找到的元素的检查状态:
$("input[name=recipients\\[\\]]").prop('checked', function(_, checked) {
return !checked;
});
这是我能想到的最好的办法。
$('#selectAll').change(function () {
$('.reportCheckbox').prop('checked', this.checked);
});
or
$checkBoxes = $(".checkBoxes");
$("#checkAll").change(function (e) {
$checkBoxes.prop("checked", this.checked);
});
or
<input onchange="toggleAll(this)">
function toggleAll(sender) {
$(".checkBoxes").prop("checked", sender.checked);
}
最基本的例子是: //获取DOM元素 var checkbox = document.querySelector('input'), button = document.querySelector('button'); //在按钮上绑定“cilck”事件 按钮。addEventListener(“点击”,toggleCheckbox); //当单击按钮时,切换复选框 函数toggleCheckbox () { 复选框。checkbox.checked = ! }; < input type = "复选框”> < / > <按钮切换复选框按钮>
下面是一个jQuery的方法来切换复选框,而不必选择html5 &标签的复选框:
<div class="checkbox-list margin-auto">
<label class="">Compare to Last Year</label><br>
<label class="normal" for="01">
<input id="01" type="checkbox" name="VIEW" value="01"> Retail units
</label>
<label class="normal" for="02">
<input id="02" type="checkbox" name="VIEW" value="02"> Retail Dollars
</label>
<label class="normal" for="03">
<input id="03" type="checkbox" name="VIEW" value="03"> GP Dollars
</label>
<label class="normal" for="04">
<input id="04" type="checkbox" name="VIEW" value="04"> GP Percent
</label>
</div>
$("input[name='VIEW']:checkbox").change(function() {
if($(this).is(':checked')) {
$("input[name='VIEW']:checkbox").prop("checked", false);
$("input[name='VIEW']:checkbox").parent('.normal').removeClass("checked");
$(this).prop("checked", true);
$(this).parent('.normal').addClass('checked');
}
else{
$("input[name='VIEW']").prop("checked", false);
$("input[name='VIEW']").parent('.normal').removeClass('checked');
}
});
http://www.bootply.com/A4h6kAPshx