不能给ajax加载的数据javascript

cannot give javascript to data loaded by ajax

本文关键字:数据 javascript 加载 ajax 不能      更新时间:2023-09-26

我有一个问题,我有js函数加载数据与ajax,加载ajax后,我得到一些数据,所以显然元素被添加到DOM后准备处理程序被触发,它的html数据

例如,我想加载test.php:

<button class='test' data-id='1' >SEND</button> //<--this data from test.php by ajax load

然后我输入一些脚本到外部js:

$('.test').click(function(){
   var id = $(this).data('id');
   alert(id);
});

脚本不工作,但如果我把内部javascript在test.php脚本是工作,我希望你能理解并帮助我

将事件处理程序委托给绑定时存在的元素

$(document).on('click', '.test', function(){
   var id = $(this).data('id');
   alert(id);
});

最好使用最接近的非动态父元素(最接近的未加载ajax的父元素)

我想你忘记准备好文件了

$(document).ready(function(){ 
$('.test').click(function(){
   var id = $(this).data('id');
   alert(id);
});
};