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

前端 未结 4 2053
余生分开走
余生分开走 2021-01-05 12:42

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

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

Everything was working

相关标签:
4条回答
  • 2021-01-05 12:52

    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
    }
    
    0 讨论(0)
  • 2021-01-05 12:53

    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)

    0 讨论(0)
  • 2021-01-05 12:55
    $('input:checkbox:not("#thatCheckboxId")').click(function(event) {
      // DO STUFF HERE
    }
    

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

    0 讨论(0)
  • 2021-01-05 13:08

    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

    0 讨论(0)
提交回复
热议问题