jQuery: Selecting by class and input type

前端 未结 6 1329
忘了有多久
忘了有多久 2021-01-30 05:51

I would like to select a set of elements that are both of a certain input type (say, a checkbox) and have a certain class using jQuery. However, when I try the following:

6条回答
  •  忘掉有多难
    2021-01-30 06:33

    Your selector is looking for any descendants of a checkbox element that have a class of .myClass.

    Try this instead:

    $("input.myClass:checkbox")
    

    Check it out in action.

    I also tested this:

    $("input:checkbox.myClass")
    

    And it will also work properly. In my humble opinion this syntax really looks rather ugly, as most of the time I expect : style selectors to come last. As I said, though, either one will work.

提交回复
热议问题