Jquery 选择器获取所有带有 ID 模式的选择下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19095607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery selector to get all select dropdowns with ID pattern
提问by Ray S.
What is the simplest way to iterate through all select drop downs with ID's matching a pattern using jquery. for example:
使用 jquery 遍历 ID 匹配模式的所有选择下拉列表的最简单方法是什么?例如:
<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>
to iterate through the first 3 selects only.
仅迭代前 3 个选择。
回答by Anton
Try this with attribute-starts-with-selector/
用attribute-starts-with-selector/试试这个
$('select[id^="begin"]').each(function () {
console.log(this.id);
});
or you could use attribute-ends-with-selector
或者你可以使用 attribute-ends-with-selector
$('select[id$="end"]').each(function () {
console.log(this.id);
});
Update
更新
To select the first 3 you can use :lt(3)
like this
要选择前 3 个,您可以:lt(3)
像这样使用
$('select[id^="begin"]:lt(3)').each(function () {
console.log(this.id);
});
Update
更新
To combine the selectors you can do this
要组合选择器,您可以执行此操作
$('select[id^="begin"][id$="end"]').each(function () {
console.log(this.id);
});
If you want to select an element with id that starts with begin ORend you can do this using ,
to get two different selectors
如果你想选择一个 id 以 begin ORend开头的元素,你可以使用,
获取两个不同的选择器来做到这一点
$('select[id^="begin"],select[id$="end"]').each(function () {
// ^
console.log(this.id);
});
回答by Arun P Johny
use attribute starts with selector, then use .each()to iterate through them
use 属性以 selector 开头,然后使用.each()遍历它们
$('select[id^=begin_]').each(function(){...})
回答by Murali Murugesan
Try using attribute starts with selector
尝试使用以选择器开头的属性
$("select[id^='begin_'").each(function(){
//your stuff
})