简体   繁体   English

数据表,点击分页不起作用

[英]datatables, with pagination on click doesn't work

I have this code in the bottom of the page: 我在页面底部有以下代码:

$('.checkbox-class:checkbox').on('click', function () {
    alert($(this).val());
});

I have a datatable with a td with this input: 我有一个带有此输入的td的数据表:

<input class="checkbox-class" type="checkbox" name="compartir[]" value="{{$row->id}}">

I can see the alert on each click of the checkbox of the first page, but when I move to the 2nd page, the script is not listening this clicks. 我可以在第一页的复选框的每次单击上看到警报,但是当我移至第二页时,脚本不会监听此单击。 But, if I reload the page, and move first to the 2nd page, then I do my first clicks, javascript can catch them well, but if I move to the 1st page, javascript doesn't listen them (on that, the 1st page) 但是,如果我重新加载页面,然后先移至第二页,那么我会进行第一次单击,javascript可以很好地捕获它们,但是,如果移至第一页,则javascript不会监听它们(在第一页上,页)

I don't get it whay is happening this. 我不明白这是怎么回事。

EDIT: A fiddle about it: 编辑:关于它的小提琴:

https://codepen.io/anon/pen/EBjNPW https://codepen.io/anon/pen/EBjNPW

You can use delegation for the event by attaching it to the table itself and only firing on the specific class type, I take it you are using JQuery as you have it in your example above, so this should serve your needs, notice how the 2nd parameter of .on is the className of the checkbox items in the table, this will then fire anytime a checkbox with that className inside the parent table is clicked: 您可以通过将事件附加到表本身并仅触发特定的类类型来使用事件的委托,我认为您正在使用JQuery,就像您在上面的示例中一样,因此这应该满足您的需求,请注意第二.on参数是表中复选框项的className,那么只要单击父表中具有该className的复选框,它将在任何时候触发:

$('#yourTable').on('click', '.checkbox-class:checkbox', function(){
    alert($(this).val());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM