我正在使用jquery datatables插件。我有许多非顺序或缺少id的数据库行:例如
id actual row
1 1
2 2
3 3
..
9 9
11 10
我在每行的开头放置了一个按钮,onclick将抓取并将该行发送到另一个函数以进行进一步处理。遵循datatables格式,HTML看起来像:
<table id="myDataTable">
<thead>
<tr>
<th>SEND TO</th>
<th>ROW</th>
<th>id</th>
<th>email</th>
<th>notes</th>
.......
</thead>
<tbody>
<tr id="1">
<td><button value="1" name="button1">PROCESSING</button></td>
<td>1</td>
<td>1</td>
.....
我的javascript看起来像:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('#myDataTable').dataTable();
});
$(document).ready(function () {
$('button[name=button1]').click(function(){
var id= $(this).attr("value");
console.log(id);
window.location.href="AjaxController/sendToProcess/"+id;
});
});
</script>
直到第10行(id 11),一切都按预期工作。超越此点击功能似乎不会触发(没有任何内容记录到控制台)。我在控制台中看不到任何错误。为什么会发生这种情况?如何解决?
答案 0 :(得分:3)
看起来你正在处理动态元素,因为你有一个分页表
您需要使用事件委派
$(document).on('click', 'button[name=button1]', function(){
var id= $(this).attr("value"); //or $(this).closest('tr').attr('id')
console.log(id);
window.location.href="AjaxController/sendToProcess/"+id;
});