jQuery选择器帮助-如何查找ID以特定字符开头和结尾的元素 [英] jQuery selector help - how to find element whose ID starts and ends with specific characters

查看:682
本文介绍了jQuery选择器帮助-如何查找ID以特定字符开头和结尾的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态创建的页面.它可以有许多具有ID的不同单选按钮,如下所示:

I have a page that's created dynamically. It can have a number of different radio buttons with ID's like so:

<input type="radio" id="cc-radio-opt-0" />
<input type="radio" id="cc-radio-opt-1" />
<input type="radio" id="gc-radio-opt-0" />
<input type="radio" id="gc-radio-opt-1" />

为了编写更改功能,我正在做类似这样的事情:

In order to write change functions, I'm doing something like this:

$('[id^=cc-radio-opt-]').live("change", function() {
    var idx = $(this).attr('id').split('-').pop(); 
});

这很好.但是现在,在该更改函数中,我需要隐藏一堆其他相关字段.它们的命名如下:

This works well. But now, inside that change function, I need to hide a bunch of other related fields. They are named like this:

<input type="text" id="cc-number-0" />
<input type="text" id="cc-month-0" />
<input type="text" id="gc-number-0" />
<input type="text" id="gc-month-0" />
<input type="text" id="cc-number-1" />
<input type="text" id="cc-month-1" />
<input type="text" id="gc-number-1" />
<input type="text" id="gc-month-1" />

我需要能够获取所有ID开头为"cc"并以与单击的单选按钮相同的数字结尾的字段(由idx变量确定.我知道我可以获取所有字段通过执行以下操作以"cc"开头:

I need to be able to grab all the fields whose IDs begin with "cc" and end with the same number as the radio button that's been clicked (determined by the idx variable. I know I can get all the fields that start with "cc" by doing this:

$('[id^=cc-]');

但是我怎么能表明他们需要以idx结尾呢?换句话说,如果单击cc-radio-opt-1单选,如何仅获取cc-number-1cc-month-1?

But how can I also indicate that they need to end with whatever idx is? In other words, if the cc-radio-opt-1 radio is clicked, how can I get only cc-number-1 and cc-month-1?

推荐答案

嗯,您快到了... $('[id^=cc-][id$=1]')

Well, you were almost there... $('[id^=cc-][id$=1]')

演示: http://jsfiddle.net/pavloschris/nr3ad/

这篇关于jQuery选择器帮助-如何查找ID以特定字符开头和结尾的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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