如何遍历每一行并收集数据

时间:2011-06-16 11:07:20

标签: javascript jquery

表中的行(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. 输入类型=“按钮”
  5. 如何遍历每一行并使用jquery从select和input中收集两个数组中的数据?

2 个答案:

答案 0 :(得分: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'));
  });
});

答案 1 :(得分:0)

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

相关问题