开发者

jQuery - How to select all checkboxes EXCEPT a specific one?

开发者 https://www.devze.com 2023-04-07 01:45 出处:网络
I have a jQuery selector that looks like this ... $(\"input:checkbox\").click(function(event) { // DO STUFF HERE

I have a jQuery selector that looks like this ...

$("input:checkbox").click(function(event) {
  // DO STUFF HERE
}

Everything was working well until I was asked to add another checkbox that has nothing to do with the original checkboxes. How do I cre开发者_StackOverflow中文版ate a selector for all checkboxes except for one? Thank you.


$('input:checkbox:not("#thatCheckboxId")').click(function(event) {
  // DO STUFF HERE
}

Just give the checkbox in question a unique id=""


You can do the following:

$("input:checkbox").not(":eq(0)")

being the "0" the index of your desired checkbox (in this case the first on the DOM)


Well.. we need to know more about the HTML for a specific answer, but there are two methods that I can think of.

1) if you know the name of the one that you don't want then remove that one with not

$("input:checkbox").not('#the_new_checkbox_id').click(function(event) {
  // DO STUFF HERE
}

2) use something that the correct inputs have in common to select them.

$("input:checkbox[name=whatItIsNamed]").click(function(event) {
  // DO STUFF HERE
}


If all the checkboxes are within another element, you can select all checkboxes within that element.

Here is an example with one parent element.

<div class="test"><input type="checkbox"><input type="checkbox"></div>

The jQuery below also works with multiple divs as well:

<div class="test"><input type="checkbox"><input type="checkbox"></div>
<div class="test"><input type="checkbox"><input type="checkbox"></div>

jQuery:

$('.test input:checkbox')

That selects just the checkboxes within any element with class "test".

-Sunjay03


i used this - dont want to select the checkbox named with "subscription" so i used "$(':checkbox:not(checkbox[name=nameofyourcheckboxyoudontwantto]').each(function(){"

   $('#checked_all').click(function (event) {
    var checkbox = $(".compare_checkbox").is(":checked");
    if (checkbox) {
        // Iterate each checkbox
        $(':checkbox:not(:checkbox[name=subscription])').each(function () {

            this.checked = false;
        });
    } else {
        $(':checkbox:not(:checkbox[name=subscription])').each(function () {
            this.checked = true;
        });
    }  });

enter image description here

0

精彩评论

暂无评论...
验证码 换一张
取 消