通过从Datatable中选择的表格行中获取正确的ID,我遇到了一些麻烦。我用来查看从JS脚本返回的内容的警报只显示选择的第一行中的相同ID,无论表列表中有多少行,当然每个ID的ID始终不同(来自Mysql)。混乱。即使通过提神仍然是一样的。在我的代码中必须是一个小bug。任何提示?这是:
PHP行输出通过JSON编码:
$('#delbtn').click( function () {
if ( $('tr.selected').hasClass('selected') ) {
var id = $('td button[type=button]').attr('data-id');
var data = {'id': id };
alert(id);
// $.ajax({...
Jquery的:
<div class="panel-body">
<button type="button" id="reloadbtn" class="btn btn-default">Refresh</button>
<button type="button" id="printbtn" class="btn btn-primary">Print</button>
<button type="button" id="delbtn" class="btn btn-danger">Delete</button>
</div>
<div class="panel-body">
<div class="table-responsive">
<table id="tablenews" class="display compact table table-striped table-bordered table-hover ">
<thead>
<tr>
<th>E-Mail</th>
<th>IPv4</th>
<th>Created</th>
<th></th>
<th></th>
</tr>
</thead>
{{1}}
答案 0 :(得分:1)
您可以尝试这样:
$('#button').click( function () {
if ( $('tr.selected').hasClass('selected') ) {
alert($('tr.selected button.delbtn').attr('data-id'));
}
else {
alert('Please select a table row first');
}
});
答案 1 :(得分:1)
也许这会有所帮助:
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
} );
$('#button').click( function () {
if ( $('tr.selected').hasClass('selected') ) {
var id = $('tr.selected button.delbtn').attr('data-id');
var data = 'id=' + id ;
alert(id);
// $.ajax({
// type: "POST",
// url: "newsdelrow.php",
// data: data,
// cache: false,
// success: function(response) {
// alert(response);
// }
// });
// table.row('.selected').remove().draw( false );
}else { alert('Please select a table row first'); }
} );
$("#example tbody").on('click', '.delbtn', function(){
var id = $(this).attr('data-id');
var data = 'id=' + id ; // var data = {'name': name } can be used instead
alert(id);
});
} );