jQuery,遍历表格每个单元格数据。
<table class="table table-hover table-bordered"> <thead> <tr> <th ><input name="selectAll" type="checkbox" class="J_selectAll"></th> <th>药名</th> <th>剂量</th> <th>次数/日</th> </tr> </thead> <tbody class="J_tbody"> <tr data-id = "1"> <td> <input class="J_subCheck" type="checkbox" name="subCheck" > </td> <td>1号药</td> <td>1片</td> <td>1</td> </tr> <tr data-id = "2"> <td> <input class="J_subCheck" type="checkbox" name="subCheck" > </td> <td>2号药</td> <td>2片</td> <td>2</td> </tr> <tr data-id = "3"> <td> <input class="J_subCheck" type="checkbox" name="subCheck" > </td> <td>1号药</td> <td>3片</td> <td>3</td> </tr> <tr data-id = "4"> <td> <input class="J_subCheck" type="checkbox" name="subCheck" > </td> <td>4号药</td> <td>4片</td> <td>4</td> </tr> </tbody> </table>
药名 | 剂量 | 次数/日 | |
---|---|---|---|
1号药 | 1片 | 1 | |
2号药 | 2片 | 2 | |
1号药 | 3片 | 3 | |
4号药 | 4片 | 4 |
var tr = $('.J_tbody').children('tr'), list =[], id, medicinal, dosage, unit, times; tr.each(function(){ var tdArr = $(this).children(), id = $(this).attr('data-id'), medicinal = tdArr.eq(1).text(), dosage = tdArr.eq(2).text(), times = tdArr.eq(3).text(), trList = {}; //trList对象一定要声明在each()函数里,否者压入栈中的数据将会是最后一条数据乘以4。 jQuery.extend(trList, { id: id, medicinal: medicinal, dosage: dosage, times: times }); list.push(trList); });