6

checkbox無効な要素を除くすべての要素を選択したいのですが、

これは私のHTMLです

<input id="chkSelectAll" class="checkbox" type="checkbox" />Select All

<div id="ContentPlaceHolder1_listItem_pnlDis_0">
    <input id="checkApproved" type="checkbox" name="checkApproved" checked="checked" disabled="disabled">
</div>
<div id="ContentPlaceHolder1_listItem_pnlDis_8" class="dis">
    <input id="checkApproved" type="checkbox" name="ctl00$ContentPlaceHolder1$listItem$ctrl8$checkApproved">
</div>

jQuery

$('#chkSelectAll').click(function () {
    var checked_status = this.checked;
    //   alert(checked_status);
    $('div#item input[type=checkbox]').each(function () {
        this.checked = checked_status;
    });
})

すべての要素を選択するために機能していcheckboxますが、無効になっている要素をスキップしたいです。

どうやってやるの?

4

8 に答える 8

17

not() を使用して、無効な属性を持つものを除外します。

$('#chkSelectAll').click(function () {
    var checked_status = this.checked;

    $('div#item input[type=checkbox]').not("[disabled]").each(function () {
               this.checked = checked_status;
    });
});

より簡潔に

$('#chkSelectAll').click(function () {
    var checked_status = this.checked;
    $('div#item input[type=checkbox]').not(":disabled").prop("checked", checked_status);
});
于 2013-05-08T19:15:23.663 に答える
3

次のように短くすることができます

$('#chkSelectAll').click(function() {
    $('div#item :checkbox:not(:disabled)').prop('checked', this.checked);
});

http://jsfiddle.net/hRc4a/

于 2013-05-08T19:25:18.740 に答える
2
$('#chkSelectAll').click(function () {
    var checked_status = this.checked;

    $('div#item input[type=checkbox]').each(function () {
           if (!this.disabled) 
               this.checked = checked_status;
    });
});

または各ループなし:

$('#chkSelectAll').on('click', function() {
    var checked_status = this.checked;

    $('div#item input[type=checkbox]').prop('checked', function(i, prop) {
         return this.disabled ? prop : checked_status;
    });
});
于 2013-05-08T19:14:16.290 に答える
0

または、次のように :not セレクターを使用することもできます。

$('#chkSelectAll').click(function () {
    var checked_status = this.checked;
    $('div#item input[type=checkbox]:not(:disabled)').each(function () {
               this.checked = checked_status;
    });
});
于 2013-05-08T19:21:35.377 に答える
0

私は個人的にお勧めします:

$('#chkSelectAll').change(function(){
    var self = this;
    $('input:checkbox').filter(function(){
        return !this.disabled
    }).prop('checked',self.checked);
});

JS フィドルのデモ

参考文献:

于 2013-05-08T19:31:20.330 に答える
0
    $('#chkSelectAll').click(function () {
        var checked_status = this.checked;
        //   alert(checked_status);
        $('div#item input[type=checkbox]').each(function () {
             if(!$(this).is(':disabled'){
                 this.checked = checked_status;
             }

        });
    })
于 2013-05-08T19:17:28.937 に答える