[英]jQuery: select all inputs with unique id (Regex/Wildcard Selectors)
I have some textboxes on a webform that have ids like this: 我在webform上有一些文本框,有这样的ID:
txtFinalDeadline_1
txtFinalDeadline_2
txtFinalDeadline_3
txtFinalDeadline_4
In my jQuery how do I find all of those in order to assign a value to them. 在我的jQuery中,如何找到所有这些以便为它们赋值。 Before I had the underscore and they were all named txtFinalDeadline I could do this and it worked. 在我有下划线之前,他们都被命名为txtFinalDeadline,我可以做到这一点并且有效。
$(this).find("#txtFinalDeadline").val(formatDate);
However, that was when they were all named the same thing. 然而,那时他们都被命名为同一件事。 Now I have the _x after the name and I'm not sure how to go about assigning that same value as before to them. 现在我的名字后面有_x,我不知道如何分配与之前相同的值。
I don't know if I can guarantee "in order", but this elector should work: 我不知道我是否可以保证“按顺序”,但这位选民应该工作:
$(this).find('input[id^=textFinalDeadline_]').val(formatDate);
Even then, jQuery is optimized to handle id
s in a special way, and may only process 1 id. 即使这样,jQuery也会以特殊方式进行优化以处理id
,并且可能只处理1个id。
A better way would be to have these textfields use a class (perhaps date
) so you could just use the '.date'
selector. 更好的方法是让这些文本字段使用一个类(可能是date
),这样你就可以使用'.date'
选择器。
There are multiple ways to do a regex or wildcard select: 有多种方法可以进行正则表达式或通配符选择:
Using jQuery selector's inline wildcards:
使用jQuery选择器的内联通配符:
$('#textFinalDeadline_\\S*').val(formatDate);
update: I noticed that this method is no longer working since jQuery 1.3.2. 更新: 我注意到自jQuery 1.3.2以来这个方法已不再有效。
Using CSS3 selectors: 使用CSS3选择器:
$('input[id^=textFinalDeadline_]').val(formatDate);
Using .filter()
& match()
: 使用.filter()
和match()
:
$('input').filter(function(){
return this.id.match(/txtFinalDeadline_*/);
}).val(formatDate);
At last you might want to use a regex selector . 最后,您可能想要使用正则表达式选择器 。
$('input:regex(id, txtFinalDeadline_*)').val(formatDate);
儿童选择器会帮助你吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.