jQuery / JavaScript Selector OR || [英] jQuery/JavaScript Selector OR ||

查看:77
本文介绍了jQuery / JavaScript Selector OR ||的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

多个值的jQuery属性选择器

我有这个

$('input:radio[name=foo]').change(function() {
    blah();
});

$('input:radio[name=bar]').change(function() {
    blah();
});

我可以这样做吗?

$('input:radio[name=foo||bar]').change(function() {
    blah();
});

如果我还有另一个?

$('input:radio[name=foo||bar||foobar]').change(function() {
    blah();
});

我不能使用理想的类,因为这是一个自定义框架(不是我的)
生成覆盖我班级的HTML。另外,没有id选择器可用
只是name属性。我觉得我不知道做了不同的功能,但是想提出一个问题,看看是否有人可以在这个框外思考。

I can't use class which would be ideal as this is a custom framework (not mine) that generates the HTML which is overriding my class. Also there is no id selector available just the name attribute. I think I'm stuck doing separate functions but wanted to pose the question to see if anyone can think outside the box on this one.

注意:这个项目使用jQuery 1.3 .2我还不能升级。在你说什么之前,是的,我和你一起升级......

Note: this project uses jQuery 1.3.2 and I can't upgrade just yet. And before you say anything, yeah I'm with you on upgrading...

推荐答案

没有'或'字符,但你可以使用多个选择器 - 你只需用逗号分隔它们:

There is no 'or' character, but you can use multiple selectors - you just separate them by commas:

$('input:radio[name=foo], input:radio[name=bar]').change(function() {
    blah();
});

或者更好的是,使用类选择器,因为这正是它们的设计目标。

Or better yet, use a class selector as this is exactly what they were designed for.

这篇关于jQuery / JavaScript Selector OR ||的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆