使用JQuery,如何根据另一个名称选择一个元素? [英] Using JQuery, how do you select one element based on another's name?
本文介绍了使用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());
});
这篇关于使用JQuery,如何根据另一个名称选择一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文