我有一个由 ajax 调用填充的数据表。代码如下:
oTable = $('#lenderList').dataTable(
{
bServerSide: true,
bProcessing: true,
searching: true,
sAjaxSource: "loanAdminAjax?ajax=true&searchCol="+$('#category').val(),
sServerMethod: 'POST',
sPaginationType: "full_numbers",
columnDefs:[
{
targets:8,
checkboxes:{
selectrow:true
}
}
],
aoColumns: [
{
"sName": "loanApplicationNumber",
mData: "loanApplicationNumber"
},
{
"sName": "name",
mData: "name"
},
{
"sName": "submissionDate",
mData: "submissionDate"
},
{
"sName": "kycEmailId",
mData: "kycEmailId"
},
{
"sName": "appVersion",
mData: "appVersion"
},
{
"sName": "documentStatus",
mData: "documentStatus"
},
{
"sName": "latestRemark",
mData: "latestRemark"
},
{
"sName": "appName",
mData: "appName"
},
{
nData: "salaryTransaction",
render: function ( salaryTransaction, type, row ) {
if ( type === 'display' ) {
return '<input type="checkbox" class="editor-active">';
}
return salaryTransaction;
},
className: "dt-body-center"
}
],
dom: '<"button">lfrtip'
}
);
});
我必须考虑用户选中复选框的相应行的所有贷款申请号。因此,我可以在单击按钮时触发事件,但如何获取单击复选框的那些行的贷款申请号的值。
我是 JavaScript 新手。请告诉我如何实现这一目标。
最佳答案
我有这样的想法:循环遍历表
的所有行
,并测试该行<的
是否复选框
是否有效检查
。如果是,则提取所需单元格的值
!
试试这个:
$(document).ready(function(){
$("button").click(function(){
oTable = $("#lenderList").dataTable(); // Get the datatable,
var loanApplicationNumbers = []; // An array that will contain the "loan application numbers"
oTable.$('tr').each(function(index,rowhtml){ //Loop through the table rows
//Check the state of the checkbox
var checked= $('input[type="checkbox"]:checked',rowhtml).length;
if (checked==1){
//If the checkbox is checked, then add the inner text of the cell to the array
loanApplicationNumbers.push(rowhtml.children[1].innerText);
}
});
console.log(loanApplicationNumbers); //Do whatever you want
});
});
注意: rowhtml.children
是该行中单元格的Array
,如果您想获取第一个单元格的值,你应该做 rowhtml.children[0].innerText
。
关于javascript - 如何传递在 jQuery 数据表中选中复选框的行列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544140/