0
我想要使用复选框选择表格行中每个单元格中的值。 场景:无论何时用户单击显示表按钮,我的页面都会动态加载表格中的一些数据,其中包含复选框,项目名称,项目代码,数量,已拒绝和已接受等列。现在,我想要在用户单击名为“save”的按钮时获取所选行的值。使用jquery获取表行中所有单元格的值
<script type="text/javascript">
$(document).ready(function() {
$("#tablediv").hide();
$("#showTable").click(function(event){
$.post('PopulateTable',{grn : $('#grn').val()},function(responseJson) {
if(responseJson!=null){
$("#itemtable").find("tr:gt(0)").remove();
\t var table1 = $("#itemtable");
\t $.each(responseJson, function(key,value) {
\t var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
\t rowNew.children().eq(0).html('<input type="checkbox" />');
\t rowNew.children().eq(1).text(value['itemname']);
\t rowNew.children().eq(2).text(value['itemcode']);
\t rowNew.children().eq(3).text(value['supplier']);
\t rowNew.children().eq(4).text(value['receivedqty']);
\t rowNew.children().eq(5).html('<input type="text" class="tb2"/>');
\t rowNew.children().eq(6).html('<input type="text" class="tb2"/>');
\t rowNew.children().eq(7).html('<input type="text" class="tb2"/>');
\t rowNew.appendTo(table1);
\t });
}
});
$("#tablediv").show();
});
});
<br/>
<div id="tablediv">
<table cellspacing="0" id="itemtable" align="center">
<tr>
\t <td><input type="checkbox" /></td>
<th scope="col">Item name</th>
<th scope="col">Item code</th>
<th scope="col">Supplier</th>
<th scope="col">Received qty</th>
<th scope="col">Accepted qty</th>
<th scope="col">Rejected qty</th>
<th scope="col">Remarks</th>
</tr>
</table>
</div>
$(document).ready(function(){
\t // code to read selected table row cell data (values).
$("#itemtable").on('click','.btnSelect',function(){
// get the current row
alert("i am inside select");
// get the current row
var currentRow=$(this).closest("tr");
var col1=currentRow.find("td:eq(0)").text(); // get SI no from checkbox
var col2=currentRow.find("td:eq(1)").text(); // get item name
var col3=currentRow.find("td:eq(2)").text(); // get item code
var col4=currentRow.find("td:eq(3)").text(); // get supplier
var col5=currentRow.find("td:eq(4)").text(); // get received qty
var col6=currentRow.find("td:eq(5)").text(); // get accepted qty
var col7=currentRow.find("td:eq(6)").text(); // get rejected qty
var col8=currentRow.find("td:eq(7)").text(); // get remarks
var data=col1+"\n"+col2+"\n"+col3+"\n"+col4+"\n"+col5+"\n"+col6+"\n"+col7+"\n"+col8;
alert(data);
});
});
<!--btnSelect is class of checkbox -->
正如你所提到的,我已经添加了复选框的名称,我感到获取价值。你在哪里提到表名/ id来获取特定选定表行的值 – Jsel
是否只有表标题,没有其他行?你看看这个例子吗? –
将被动态加载使用上面提到的showTable jquery – Jsel