jQuery:选择具有唯一ID的所有输入(正则表达式/通配符选择器) [英] jQuery: select all inputs with unique id (Regex/Wildcard Selectors)
问题描述
我在网络表单上有一些文本框,其ID如下:
I have some textboxes on a webform that have ids like this:
txtFinalDeadline_1
txtFinalDeadline_2
txtFinalDeadline_3
txtFinalDeadline_4
在我的jQuery中,如何找到所有这些以便为它们分配值.在我使用下划线并将它们全部命名为txtFinalDeadline之前,我可以做到这一点,并且确实有效.
In my jQuery how do I find all of those in order to assign a value to them. Before I had the underscore and they were all named txtFinalDeadline I could do this and it worked.
$(this).find("#txtFinalDeadline").val(formatDate);
然而,那是当他们都被命名为同一个东西的时候.现在,名字后面有_x,我不确定如何为他们分配与以前相同的值.
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.
推荐答案
我不知道我是否可以保证按顺序",但该选民应该工作:
I don't know if I can guarantee "in order", but this elector should work:
$(this).find('input[id^=textFinalDeadline_]').val(formatDate);
即使那样,jQuery仍已优化为以特殊方式处理id
,并且只能处理1个id.
Even then, jQuery is optimized to handle id
s in a special way, and may only process 1 id.
一种更好的方法是让这些文本字段使用一个类(也许是date
),这样您就可以使用'.date'
选择器.
A better way would be to have these textfields use a class (perhaps date
) so you could just use the '.date'
selector.
这篇关于jQuery:选择具有唯一ID的所有输入(正则表达式/通配符选择器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!