【问题标题】:How to iterate through each row and collect data如何遍历每一行并收集数据
【发布时间】:2011-06-16 11:07:19
【问题描述】:

表中的行 (id='row_property_'+number) 包含四列:

  1. 选择元素(id='multiple_object_type_'+number)
  2. input type="text" (id='multiple_instance_id_'+number)
  3. 嵌套表(id='table_properties_'+number)
  4. 输入类型="按钮"

如何遍历每一行并使用jquery从select和input中收集两个数组中的数据?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

好的,向 select 和 input 元素添加类,使表格看起来像这样:

<table id="myTable">
  <tr>
    <td><select class="rowSelect"></select></td>
    <td><input type="text" class="rowInput" /></td>
    ... etc ...
  </tr>
</table>

然后你可以像这样在 jquery 中获取每一行的值:

$(function(){
  $('#myTable tr').each(function(){
    alert('select value is '+$(this).find('select.rowSelect'));
    alert('input value is '+$(this).find('input.rowInput'));
  });
});

【讨论】:

    【解决方案2】:
    var selectArray = $('table tr td > select').map(function() {
        return $(this).val();
    }).get();
    
    var inputArray = $('table tr td > input:text').map(function() {
        return $(this).val();
    }).get();
    

    这可能会做你想要的。

    JSFiddle Example

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 2019-12-15
      • 2013-05-20
      • 2019-11-24
      • 2019-09-07
      • 1970-01-01
      相关资源
      最近更新 更多