Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

jquery - Select all checkbox elements except disabled ones

I want to select all checkbox elements expect disabled ones,

this is my 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;
    });
})

it's working for selecting all checkbox elements but I want to skip disabled ones.

How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use not() to exclude things with a disabled attribute.

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

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

more concise

$('#chkSelectAll').click(function () {
    var checked_status = this.checked;
    $('div#item input[type=checkbox]').not(":disabled").prop("checked", checked_status);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...