【问题标题】:About jQuery .each or other iteration关于 jQuery .each 或其他迭代
【发布时间】:2009-09-04 11:33:34
【问题描述】:

如果表有这种结构,如何遍历它:

<table>
  <tr>
    <td><input class = "fire"> ... </td>
    <td><input class = "water"> ... </td>
  </tr>
  <tr>
    <td><input class = "fire"> ... </td>
    <td><input class = "water"> ... </td>
  </tr>
  <tr>
    <td><input class = "fire"> ... </td>
    <td><input class = "water"> ... </td>
  </tr>
</table>

我需要在每次迭代中这样做:

iterating:
     $("fire").val(newValue1); 
     $("water").val(newValue2);

【问题讨论】:

    标签: javascript jquery iterator


    【解决方案1】:

    查询类名:

     $(".fire").val(newValue1); 
     $(".water").val(newValue2);
    

    或者:

    $("table input.fire").each(function (i) {
      $(this).val("input " + i);
    });
    
    $("table input.water").each(function (i) {
      $(this).val("input " + i);
    });
    

    【讨论】:

      【解决方案2】:

      为什么需要迭代?

      $('tr').each( function(){
          $(this).find('input.fire').val(newValue1);
          $(this).find('input.water').val(newValue2);
      });
      

      【讨论】:

      • 因为我总是需要在每个 上设置不同的值,其中是火和水类
      【解决方案3】:

      如果您需要每次迭代都有自己的价值,您可以这样做:

      $("tr").each(function(i) {
          var newValue1 = "Some value"+i;
          var newValue2 = "Some other value"+i;
          $(this).find(".fire").val(newValue1);
          $(this).find(".water").val(newValue2);
      });
      

      【讨论】:

        猜你喜欢
        • 2010-12-14
        • 2012-10-17
        • 2011-12-02
        • 1970-01-01
        • 2014-09-15
        • 1970-01-01
        • 2011-09-12
        • 2011-04-09
        相关资源
        最近更新 更多