同时迭代jQuery中的两组元素 [英] Simultaneously Iterating Over Two Sets of Elements in jQuery

查看:85
本文介绍了同时迭代jQuery中的两组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,在单独的列中包含多对文本输入字段.我想遍历第一列中的输入字段,并使用这些值来设置相邻列中相应输入字段的值.

I have a table containing many pairs of text input fields in separate columns. I want to iterate through the input fields in the first column and use those values to set the value of the corresponding input field in the adjacent column.

<table>
    <tr>
         <td><input type="text" class="left" /></td>
         <td><input type="text" class="right" /></td>
    </tr>
    <tr>
         <td><input type="text" class="left" /></td>
         <td><input type="text" class="right" /></td>
    </tr>
  ...
</table>

我刚刚开始学习jQuery,所以答案很明显.到目前为止,我只有

I have just started learning jQuery so perhaps the answer is obvious. So far I have only

$("input.left").each(function() {
    // use the value of $(this) to set the 
    // value of the text field to the .right
})

推荐答案

很多方法可以做到这一点.这是一个.

Lots of ways to do this. Here's one.

$("tr").each(function() {
  $(this).find(":last-child input").val($(this).find(":first-child input").val());
});

或另一个:

$("input.left").each(function() {
  $(this).parent().nextSibling().find("input.right").val(this.value);
});

以此类推.

这篇关于同时迭代jQuery中的两组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆