我有以下几点:

$(document).ready(function()
{
    $("#select-all-teammembers").click(function() {
        $("input[name=recipients\\[\\]]").attr('checked', true);
    });                 
});

我希望id="select-all-teammembers"在选中和未选中之间切换。想法吗?那不是几十行代码吗?


当前回答

最基本的例子是: //获取DOM元素 var checkbox = document.querySelector('input'), button = document.querySelector('button'); //在按钮上绑定“cilck”事件 按钮。addEventListener(“点击”,toggleCheckbox); //当单击按钮时,切换复选框 函数toggleCheckbox () { 复选框。checkbox.checked = ! }; < input type = "复选框”> < / > <按钮切换复选框按钮>

其他回答

//this toggles the checkbox, and fires its event if it has    

$('input[type=checkbox]').trigger('click'); 
//or
$('input[type=checkbox]').click(); 

假设它是一个必须切换复选框的图像,这对我来说是可行的

<img src="something.gif" onclick="$('#checkboxid').prop('checked', !($('#checkboxid').is(':checked')));">
<input type="checkbox" id="checkboxid">

有一个更简单的方法

首先给你的复选框一个类示例'id_chk'

然后在复选框内,将控制'id_chk'复选框的状态put:

<输入类型='checkbox' onchange='js:jQuery(.id_chk)。道具(“检查”、j查询(this)、道具(“检查”)' />

就这些,希望这能有所帮助

<table class="table table-datatable table-bordered table-condensed table-striped table-hover table-responsive">
<thead>
    <tr>
        <th class="col-xs-1"><a class="select_all btn btn-xs btn-info"> Select All </a></th>
        <th class="col-xs-2">#ID</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><input type="checkbox" name="order333"/></td>
        <td>{{ order.id }}</td>
    </tr>
    <tr>
        <td><input type="checkbox" name="order334"/></td>
        <td>{{ order.id }}</td>
    </tr>
</tbody>                  
</table>

Try:

$(".table-datatable .select_all").on('click', function () {
    $("input[name^='order']").prop('checked', function (i, val) {
        return !val;
    });
});

从jQuery 1.6开始,你可以使用.prop(function)来切换每个找到的元素的检查状态:

$("input[name=recipients\\[\\]]").prop('checked', function(_, checked) {
    return !checked;
});