I have the following Javascript file:
我有以下Javascript文件:
$(document).ready(function(){
$( "#registry_donations_attributes_.0-9_data_donazione" ).datepicker({ dateFormat: 'yy-mm-dd' }).val();
});
I would take all the id that follow this rule:
我会采取遵循此规则的所有ID:
#registry_donations_attributes_0_data_donazione
#registry_donations_attributes_1_data_donazione
#registry_donations_attributes_2_data_donazione
#registry_donations_attributes_3_data_donazione
...
#registry_donations_attributes_n_data_donazione
I tried "#registry_donations_attributes_.0-9_data_donazione"
but it doen't work. I don't know jquery very well. Thank You.
我试过“#registration_donations_attributes_.0-9_data_donazione”,但它不起作用。我不太了解jquery。谢谢。
3 个解决方案
#1
0
JQuery selectors do not support regex patterns, which is apparently the way you want to specify the pattern for the id naming.
JQuery选择器不支持正则表达式模式,这显然是您想要为id命名指定模式的方式。
What you specify instead are elements with the id registry_donations_attributes_
that have the css class 0-9_data_donazione
, which does not make much sense in this context (which shouldn't make you feel bas, selector syntax can be intricate).
你指定的是id为registry_donations_attributes_的元素,它们具有css类0-9_data_donazione,这在这个上下文中没有多大意义(这不应该让你觉得卑鄙,选择器语法可能很复杂)。
If possible, assign a common css class ( like _data_donazione
) to the elements and use the selector as follows: $("._data_donazione")
.
如果可能,请为元素分配一个公共css类(如_data_donazione),并按如下方式使用选择器:$(“._ data_donazione”)。
Alternatively, use a combination of attribute prefix and suffix selector (not recommended; to be painstakingly correct you'd have to check for the number part of the id anyway):
或者,使用属性前缀和后缀选择器的组合(不推荐;要刻意纠正,你必须检查id的数字部分):
$("*[id^='registry_donations_attributes_'][id$='_data_donazione]")
#2
0
why don't you use a for loop?...
你为什么不用for循环?...
n = 10;
for (var x = 0; x < n; x++){
$( "#registry_donations_attributes_" + x + "_data_donazione").datepicker({ dateFormat: 'yy-mm-dd' }).val();
}
#3
0
The best you can do in a straight jQuery selector is to combine the starts with and ends with attribute selectors.
您可以在直接jQuery选择器中做的最好的事情是将开头与属性选择器结合起来。
var items = $("[id^='registry_donations_attributes_'][id$='_data_donazione']");
console.log(items.toArray());
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="registry_donations_attributes_0_data_donazione"></div>
<div id="registry_donations_attributes_1_data_donazione"></div>
<div id="registry_donations_attributes_2_data_donazione"></div>
<div id="not"></div>
Alternatively you could use a filter with a regular expression
或者,您可以使用带有正则表达式的过滤器
var items = $("*").filter(function() {
return this.id.match(/registry_donations_attributes_\d*_data_donazione/);
});
console.log(items.toArray());
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="registry_donations_attributes_0_data_donazione"></div>
<div id="registry_donations_attributes_1_data_donazione"></div>
<div id="registry_donations_attributes_2_data_donazione"></div>
<div id="not"></div>
#1
0
JQuery selectors do not support regex patterns, which is apparently the way you want to specify the pattern for the id naming.
JQuery选择器不支持正则表达式模式,这显然是您想要为id命名指定模式的方式。
What you specify instead are elements with the id registry_donations_attributes_
that have the css class 0-9_data_donazione
, which does not make much sense in this context (which shouldn't make you feel bas, selector syntax can be intricate).
你指定的是id为registry_donations_attributes_的元素,它们具有css类0-9_data_donazione,这在这个上下文中没有多大意义(这不应该让你觉得卑鄙,选择器语法可能很复杂)。
If possible, assign a common css class ( like _data_donazione
) to the elements and use the selector as follows: $("._data_donazione")
.
如果可能,请为元素分配一个公共css类(如_data_donazione),并按如下方式使用选择器:$(“._ data_donazione”)。
Alternatively, use a combination of attribute prefix and suffix selector (not recommended; to be painstakingly correct you'd have to check for the number part of the id anyway):
或者,使用属性前缀和后缀选择器的组合(不推荐;要刻意纠正,你必须检查id的数字部分):
$("*[id^='registry_donations_attributes_'][id$='_data_donazione]")
#2
0
why don't you use a for loop?...
你为什么不用for循环?...
n = 10;
for (var x = 0; x < n; x++){
$( "#registry_donations_attributes_" + x + "_data_donazione").datepicker({ dateFormat: 'yy-mm-dd' }).val();
}
#3
0
The best you can do in a straight jQuery selector is to combine the starts with and ends with attribute selectors.
您可以在直接jQuery选择器中做的最好的事情是将开头与属性选择器结合起来。
var items = $("[id^='registry_donations_attributes_'][id$='_data_donazione']");
console.log(items.toArray());
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="registry_donations_attributes_0_data_donazione"></div>
<div id="registry_donations_attributes_1_data_donazione"></div>
<div id="registry_donations_attributes_2_data_donazione"></div>
<div id="not"></div>
Alternatively you could use a filter with a regular expression
或者,您可以使用带有正则表达式的过滤器
var items = $("*").filter(function() {
return this.id.match(/registry_donations_attributes_\d*_data_donazione/);
});
console.log(items.toArray());
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="registry_donations_attributes_0_data_donazione"></div>
<div id="registry_donations_attributes_1_data_donazione"></div>
<div id="registry_donations_attributes_2_data_donazione"></div>
<div id="not"></div>