读取jquery中的每个表行

时间:2018-05-01 16:24:00

标签: jquery

我有这张表,其中包含有关我的图书购物车的基本信息

<table style="width:100%">
  <tr>
    <th>School No</th>
    <th>Resource Type</th> 
    <th>Resource No.</th>
    <th>Resource Title</th>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>201912621</td>
    <td>Book</td> 
    <td>099918723</td>
    <td>Sample Title</td>
    <td>10</td>
  </tr>
</table>

为了简短起见,我如何阅读表格中的每一行,并在jquery中将其输出为这样。

var row1= $("#SchoolNo").val();

1 个答案:

答案 0 :(得分:0)

function table2array(){
  var out=[];
  $('table tr').each(function(){
  	 var el=[]
  	 $(this).children('td').each(function(){
  	 	el.push($(this).text());
  	 });
  	  if(el.length){
  	   out.push(el);
  	  }
  });
  return out;
}

$('#btn_get_all').click(function(){
	var my_arr=table2array();
	console.log(my_arr);
});

$('#btn_get_one').click(function(){
	var my_arr=table2array();
	alert(my_arr[1][3]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>






<table style="width:100%" border="1">
  <tr>
    <th>School No</th>
    <th>Resource Type</th>
    <th>Resource No.</th>
    <th>Resource Title</th>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>201912621</td>
    <td>Book</td>
    <td>099918723</td>
    <td>Sample Title</td>
    <td>10</td>
  </tr>
  <tr>
    <td>201912456</td>
    <td>Cd</td>
    <td>099918745</td>
    <td>Sample Title 2</td>
    <td>11</td>
  </tr>
</table>


<input type="button" id="btn_get_all" value="get all TABLE content"></br>
<input type="button" id="btn_get_one" value="get SELL(1,3)">