使用JQuery,如何根据另一个名称选择一个元素?
问题描述:
如果我有一个Web表单一串编号字段,如发票:
If I have a bunch of numbered fields in a web form, like an invoice:
< input type="text" name="Item1" /><input type="text" name="Desc1" /><br />
< input type="text" name="Item2" /><input type="text" name="Desc2" /><br />
< input type="text" name="Item3" /><input type="text" name="Desc3" /><br />
使用JQuery,我如何选择第一个,然后从第一个引用第二个?
Using JQuery, how would I select the first, then reference the second from the first?
我有类似 $(input [id ^ ='Item'])
,但是从每个这些我需要引用相应的Desc并填充一个结果。
I have something like $("input[id^='Item']")
, which selects each Item pretty well, but from each of these I need to reference the corresponding Desc and fill it with a result.
例如说,用户在Item1和onBlur中键入内容我想复制内容到相应的Desc。
Say for instance the user types something in Item1 and onBlur I want to copy the content to the corresponding Desc.
感谢
答
是一个可能的解决方案:
This could be a possible solution:
$("input[name^=Item]").blur(function() {
var n = $(this).attr("name").match(/\d+/);
$("input[name=Desc" + n + "]").val($(this).val());
});