【问题标题】:Add data from array to empty input with jQuery使用jQuery将数据从数组添加到空输入
【发布时间】:2011-04-04 11:56:30
【问题描述】:

我有一些动态生成的输入。有些有 ID,有些没有。我还有一个 ID 数组。我需要遍历 ID 数组并将下一个可用 ID 输入到下一个输入中,而不需要任何值。 我试过了

$.each(event_array, function(intIndex, objValue){
  $('.event-data').find('.event-id').each(function(i){
    $(".event-id:empty").val(objValue);
  });
});

和

$('.event-data').find('.event-id').each(function(i){
  $(".event-id").val(event_array[i]);
});

显然第二个不搜索输入的值,第一个使用 :empty 我发现这不是我使用它的目的。

知道我需要用什么来让它工作吗? 谢谢!!

【问题讨论】:

    标签: jquery arrays input find each


    【解决方案1】:

    这将过滤.event-id 元素中没有值的元素,然后将一个函数传递给.val(),该函数返回与当前索引匹配的数组元素。

    $('.event-data .event-id').filter(function() {
        return this.value === '';  // filter items that don't have a value
    })
    .val(function(i,val) { return event_array[i]; }); // Call .val() on the resulting
                                                      //   set, using the i value of
                                                      //   each to get the next item
                                                      //   from your array
    

    将函数传递给 .val() 需要 jQuery 1.4 或更高版本。

    【讨论】:

      猜你喜欢
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多