html table代码如下:

  1. <table id="elemet_page_table" class="jsgrid-table">
  2.     <tbody>
  3.     <tr class="jsgrid-header-row">
  4.         <th class="jsgrid-header-cell" style="width: 150px;">名称</th>
  5.         <th class="jsgrid-header-cell" style="width: 50px;">操作</th>
  6.     </tr>
  7.     {% for element_page_name in element_page_list %}
  8.     <tr class="jsgrid-row" id="{{ element_page_name.id }}">
  9.         <td class="jsgrid-cell" style="width: 150px;">
  10.             <input name="name" type="text" value={{ element_page_name.name }} readonly style="border: none;outline:medium;">
  11.         </td>
  12.         <td class="jsgrid-cell jsgrid-control-field jsgrid-align-center" style="width: 50px;">
  13.             <span id="{{ element_page_name.edit_id }}" class="ti-pencil" type="button" title={{ element_page_name.id }} onclick="edit_row(this)">
  14.             </span><span id="{{ element_page_name.save_id }}" class="ti-check" type="button" title={{ element_page_name.id }} onclick="save_row(this)">
  15.             </span><span class="ti-plus" type="button" title={{ element_page_name.id }} onclick="add_row_after(this)">
  16.             </span><span class="ti-trash" type="button" title={{ element_page_name.id }} onclick="delete_row(this)"></span></td>
  17.     </tr>
  18.     {% endfor %}
  19.     </tbody>
  20. </table>
使用js遍历talbe的所有行和列

  1. $("#do_save").click(function(){
  2.     // 遍历所有的table-body数据
  3.     $('#elemet_page_table').find('tbody').each(function (i) {
  4.         var row_num=$(this).find('tr').length;
  5.         console.log("总共行数:" + row_num);
  6.         $(this).find('tr').each(function (j) {
  7.             //去除表头
  8.             if(j>0){
  9.                 //获取一行有多少列,并去除最后一个操作列
  10.                 var cell_num=$(this).find('td').length;
  11.                 console.log("单行列数:" + cell_num);
  12.                 $(this).find('td').each(function (h) {
  13.                     if(h < cell_num-1){
  14.                         console.log($(this).context.firstElementChild.value);
  15.                         }
  16.                 });
  17.                 }
  18.             });
  19.         //return false;
  20.     });
  21. });


11-19 21:56