启用/禁用复选框 [英] Toggle Checkboxes on/off

查看:67
本文介绍了启用/禁用复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

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

点击id="select-all-teammembers"以在已选中和未选中之间切换时,我想要.有想法吗?那不是几十行代码吗?

I'd like the id="select-all-teammembers" when clicked to toggle between checked and unchecked. Ideas? that aren't dozens of lines of code?

推荐答案

您可以编写:

$(document).ready(function() {
    $("#select-all-teammembers").click(function() {
        var checkBoxes = $("input[name=recipients\\[\\]]");
        checkBoxes.prop("checked", !checkBoxes.prop("checked"));
    });                 
});

在jQuery 1.6之前,当我们只有 attr()而没有

Before jQuery 1.6, when we only had attr() and not prop(), we used to write:

checkBoxes.attr("checked", !checkBoxes.attr("checked"));

但是,当将prop()应用于布尔" HTML属性时,其语义要比attr()更好,因此在这种情况下通常是首选.

But prop() has better semantics than attr() when applied to "boolean" HTML attributes, so it is usually preferred in this situation.

这篇关于启用/禁用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆