【问题标题】:how to apply each index value of an array to each value of a group of selected elements如何将数组的每个索引值应用于一组选定元素的每个值
【发布时间】:2009-11-21 14:55:15
【问题描述】:

因此,如果我有一个包含 2 个值的数组,并且我想将这些值应用于两个元素,我似乎无法找到一种方法来循环遍历所选元素并有一个计数器或其他东西来表示我要应用的数组值。

有人吗?

例如:

myTotal = new Array("7.00", "10.00");

<input class="theTotalInput"></input>
<input class="theTotalInput"></input>

所以,一旦我选择了输入,

$(".theTotalInput")

...

是的……这就是我卡住的地方。我找不到任何示例文档显示依次将值数组应用于一堆元素 - 我所看到的只是将相同的值应用于一堆元素。

感谢您的帮助。

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案1】:

    var i = 0;
    $(".theTotalInput").each(function() {
        $(this).val(myTotal[i++])
    });
    

    【讨论】:

    【解决方案2】:

    另一种可能更短的方式:

    $(["7.00","10.00"]).each(function(i, val) {
        $(".theTotalInput").eq(i).val(val);
    });
    

    或:

    $(".theTotalInput").each(function(i, el){
        $(el).val(myTotal[i]);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2014-09-29
      • 1970-01-01
      相关资源
      最近更新 更多