简体   繁体   English

.click()不适用于ajax加载的内容

[英].click() does not work with ajax loaded content

I have a Datatable which fetches the data from my server using AJAX. 我有一个数据表,该数据表使用AJAX从我的服务器中获取数据。

This code adds the data to the Datatable and also adds some buttons to it 这段代码将数据添加到数据表中,并且还向其中添加了一些按钮

                var jsdata = JSON.parse(data);
                for (var i = 0; i < jsdata.length; i++) {
                    var actions =
                            '<div class="btn-group">' +
                                '<button class="btn btn-default btn-xs" type="button" data-button=\'{"func": "func1"}\' data-process_id="' + jsdata[i][0] + '"><i class="icon-remove"></i> </button>' +
                            '</div>';
                    jsdata[i].push(actions);
                }

I also have this code which should grab a click on the created buttons (by the above script) 我也有这段代码,应该点击创建的按钮(通过上面的脚本)

$('button').click(function() {
    alert('test');
});

Unfortunately the functions does not get called. 不幸的是,这些函数没有被调用。 If i place the buttons directly (without the AJAX request) it works fine. 如果我直接放置按钮(没有AJAX请求),则效果很好。

How can i fix this? 我怎样才能解决这个问题?

Attach the event handler using on 使用on附加事件处理程序

$(document).on("click", "button",function() {
    alert('test');
});

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

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