我有以下几点:
$(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"在选中和未选中之间切换。想法吗?那不是几十行代码吗?
当前回答
//this toggles the checkbox, and fires its event if it has
$('input[type=checkbox]').trigger('click');
//or
$('input[type=checkbox]').click();
其他回答
你可以这样写:
$(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()更好的语义,所以在这种情况下通常是首选。
假设它是一个必须切换复选框的图像,这对我来说是可行的
<img src="something.gif" onclick="$('#checkboxid').prop('checked', !($('#checkboxid').is(':checked')));">
<input type="checkbox" id="checkboxid">
jQuery("#checker").click(function(){
jQuery("#mydiv :checkbox").each(function(){
this.checked = true;
});
});
jQuery("#dechecker").click(function(){
jQuery("#mydiv :checkbox").each(function(){
this.checked = false;
});
});
jQuery("#checktoggler").click(function(){
jQuery("#mydiv :checkbox").each(function(){
this.checked = !this.checked;
});
});
;)
//this toggles the checkbox, and fires its event if it has
$('input[type=checkbox]').trigger('click');
//or
$('input[type=checkbox]').click();
有一个更简单的方法
首先给你的复选框一个类示例'id_chk'
然后在复选框内,将控制'id_chk'复选框的状态put:
<输入类型='checkbox' onchange='js:jQuery(.id_chk)。道具(“检查”、j查询(this)、道具(“检查”)' />
就这些,希望这能有所帮助