我在datatable
Html代码
<table id="tbldriver" class="table table-bordered table-striped">
<thead>
<tr style="color:white;background-color:#3c8dbc">
<th>Id chauffeur</th>
<th>Chauffeur</th>
<th> </th>
</tr>
</thead>
<tbody id="tbldriverBody"></tbody>
</table>
Javascript部分
function GetFiltredDrivers() {
$.ajax({
type: "Get",
url: "/api/AccountManage/GetAllChauffeurs",
success: function (data) {
EmptyGridFilter();
for (var i = 0; i < data.length; i++) {
var chauffeur = data[i];
$('#tbldriverBody').append('<tr><td><input type="text" readonly name="FiltredDriverAgendaModel['+i+'].Id_driver_agenda" value="' + chauffeur.id + '" /> </td>'
+ '<td><input type="text" name="FiltredDriverAgendaModel[' + i + '].name_driver_agenda" readonly value="' + chauffeur.Nom + " " + chauffeur.Prenom + '" /></td>'
+ '<td><input type="checkbox" name="FiltredDriverAgendaModel[' + i + '].isDriveChecked" checked="checked" /> </td></tr>');
}
initGridDriver();
}
});
}
function initGridDriver() {
var table = $('#tbldriver').dataTable({
"processing": true,
"bPaginate": true,
"bLengthChange": false,
"bFilter": true,
"bSort": false,
"bInfo": true,
"responsive": true,
"scrollX": true,
"scrollY": "200px",
"scrollCollapse": true,
"bAutoWidth": false,
"language": { "url": "//cdn.datatables.net/plug-ins/1.10.7/i18n/French.json" },
"lengthMenu": [[50, 100, 250, 500, -1], [50, 250, 500, "Tout"]],
"destroy": true,
"columnDefs": [
{ "width": "20%", "targets": 0 },
{ "width": "50%", "targets": 1 },
{ "width": "30%", "targets": 2 }
],
"bAutoWidth": false
});
$("#tbldriver tr").css('cursor', 'pointer');
}
我添加了一个复选框列作为表格中的最后一列,问题是所有复选框属性仍为checked
,即使我取消选中它:
示例
var arr = [];
$('#tbldriver input[type=checkbox]').each(function () {
alert($(this).val());
if ($(this).val() == 'on') arr.push($(this).val());
});
console.log(arr);
所有提醒都会将on
作为文字。
所以我需要知道:
答案 0 :(得分:1)
设置复选框的值属性,并使用下面的代码获取仅选中复选框的值:
var arr = [];
$("#tbldriver input[type='checkbox']:checked").each(function () {
arr.push($(this).val());
});
console.log(arr);