jQuery选择器以获取具有ID模式的所有选择下拉列表 [英] Jquery selector to get all select dropdowns with ID pattern
本文介绍了jQuery选择器以获取具有ID模式的所有选择下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最简单的方法是遍历所有选择下拉列表,其中ID使用jquery匹配模式.例如:
What is the simplest way to iterate through all select drop downs with ID's matching a pattern using jquery. for example:
<select id="begin_1_end">...</select>
<select id="begin_32_end">...</select>
<select id="begin_42_end">...</select>
<select id="dontgetme_2_end">...</select>
<select id="begin_12_dontgetme">...</select>
仅迭代前三个选择.
推荐答案
尝试使用 attribute-starts-with-selector/
$('select[id^="begin"]').each(function () {
console.log(this.id);
});
,或者您可以使用属性-ends-with-selector >
or you could use attribute-ends-with-selector
$('select[id$="end"]').each(function () {
console.log(this.id);
});
更新
要选择前3个,您可以像这样使用:lt(3)
To select the first 3 you can use :lt(3)
like this
$('select[id^="begin"]:lt(3)').each(function () {
console.log(this.id);
});
更新
要合并选择器,您可以这样做
To combine the selectors you can do this
$('select[id^="begin"][id$="end"]').each(function () {
console.log(this.id);
});
如果您要选择ID以 OR 开头的元素,则可以使用,
来获取两个不同的选择器
If you want to select an element with id that starts with begin OR end you can do this using ,
to get two different selectors
$('select[id^="begin"],select[id$="end"]').each(function () {
// ^
console.log(this.id);
});
这篇关于jQuery选择器以获取具有ID模式的所有选择下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文