开发者

name = "this or that" in jQuery?

I have this code:

$("input[name=foo]:checked,input[name=bar]:checked")

but I would like to make it smaller with something like:

$("input[name=foo|bar]:checked开发者_Python百科")

Is there anything like that?


The shortest version, as inputs are the only things that "should" have names.

$('[name=foo],[name=bar]');

A bit more safe as inputs are the only things that can actually be checked.

$(":checked").filter('[name=foo],[name=bar]');

Exactly as safe as your original selector, but two characters shorter:

$("input:checked").filter('[name=foo],[name=bar]');

But generally I agree, there is no real need and you're better off spending your time elsewhere.


If you really want to do that, you can use James Padolsey's regex selector:

$("input:regex(name,^(foo|bar)$):checked")

But that's less clear than your original code in my opinion.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜