如何检查JQuery中的所有复选框?

时间:2012-12-23 19:26:29

标签: javascript jquery html checkbox webforms

我是jQuery的初学者,我知道之前已经问过这个问题,但我不知道为HTML做正确的方法:

<fieldset>
    <legend>ABC</legend>
    <label class="selectAllButton"><input type="checkbox" onclick="SelectAll(this)" />(select all)</label><br />
    <table>

            <tr>
            <td>
                <input id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_chkItem_0" type="checkbox" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl00$chkItem" /><label for="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_chkItem_0">Discs</label>
                <input type="hidden" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl00$pgNos" id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_pgNos_0" value="9;71;72;108;109" />
                <input type="submit" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl00$btnXRefs" value="x-refs" id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_btnXRefs_0" class="xRefButton" />
            </td>
            </tr>

            <tr>
            <td>
                <input id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_chkItem_1" type="checkbox" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl01$chkItem" /><label for="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_chkItem_1">Drums</label>
                <input type="hidden" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl01$pgNos" id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_pgNos_1" value="73" />
                <input type="submit" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl01$btnXRefs" value="x-refs" id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_btnXRefs_1" class="xRefButton" />
            </td>
            </tr>

            <tr>
            <td>
                <input id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_chkItem_2" type="checkbox" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl02$chkItem" /><label for="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_chkItem_2">Pads</label>
                <input type="hidden" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl02$pgNos" id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_pgNos_2" value="70" />
                <input type="submit" name="ctl00$MainContentPlaceHolder$CategoryRepeater$ctl00$ItemRepeater$ctl02$btnXRefs" value="x-refs" id="MainContentPlaceHolder_CategoryRepeater_ItemRepeater_0_btnXRefs_2" class="xRefButton" />
            </td>
            </tr>

    </table>
</fieldset>

我希望点击它:

<label class="selectAllButton"><input type="checkbox" onclick="SelectAll(this)" />(select all)</label><br />

并且要检查或取消选中table下的所有复选框。

我试过了:

// selects all the items in same category
function SelectAll()
{
    $(arguments[0]).siblings('table:eq(0)').find(':checkbox').attr('checked', arguments[0].checked);
}

但它没有用。

2 个答案:

答案 0 :(得分:3)

您可以使用

function SelectAll(element)
{
    $(element).closest('fieldset').find('table :checkbox').prop('checked', element.checked);
}

你的版本不起作用的原因是该表不是select-all复选框的兄弟。它是包含复选框的标签的兄弟。

答案 1 :(得分:2)

你必须做这样的事情:

$('.selectAllButton input').click(function() {
    var checked = $(this).is(':checked');
    var el = $('table input[type=checkbox]');

    if(checked) {
        el.attr('checked', 'checked');
    } else {
        el.removeAttr('checked');
    };
});

http://jsfiddle.net/yM9yf/1/

下面的代码看起来不错,更简单,有效,但它并不完全是W3C推荐的。而且我不确定它是否适用于IE6。

$('.selectAllButton input').click(function() {
   var checked = $(this).is(':checked');
   $('table input[type=checkbox]').attr('checked', checked);
});​

http://jsfiddle.net/CAYef/

有用的链接: 1. W3C Working Draft: input type=checkbox 2. W3C Wiki: checkbox 3. MSDN: checked attribute 4. MDN: input

我希望这可以帮到你。