jQuery - Multiple Selectors in a :not()?

情到浓时终转凉″ 提交于 2019-12-20 10:16:05

问题


I cant seem to get the following working:

$('input:not([type=radio][type=checkbox])').live('click', function() {
    alert("You haven't clicked a radio or checkbox!");
});

Have tried a few different syntax's, can anyone help me out on this one.

Cheers
Charlie


回答1:


You're confusing the multiple selector with the multiple attribute selector. You should write:

$("input:not([type=radio], [type=checkbox])");

The multiple attribute selector [type=radio][type=checkbox] matches the elements whose type attributes are equal to both radio and checkbox, which cannot happen in this universe.




回答2:


The syntax inside the not() is identical to the normal selector syntax, so if you want to match things that are not either of those things, you use a selector in the not that would match both (since essentially you're negating the selector). How do you do that? The same way you apply styles to multiple selectors at once in CSS, with a comma, as so:

$('input:not([type=radio],[type=checkbox])')

That should select all inputs the not [type=radio] and not [type=checkbox]




回答3:


You can also exclude items like this:

$('input').not('[type=radio], [type=checkbox]').live('click', function() {
    alert("You haven't clicked a radio or checkbox!");
});


来源:https://stackoverflow.com/questions/6166871/jquery-multiple-selectors-in-a-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!