jQuery单击事件不起作用

jQuery单击事件不起作用,jquery,html,click,Jquery,Html,Click,我的.js代码有问题 守则: <script language="javascript"> $('#firstButton').click(function(){ $.getJSON("traitementReady.php",function(data){ $("#categories").empty(); $.each(data.result, function(){ $("#categories").append(

我的
.js
代码有问题

守则:

<script language="javascript">
$('#firstButton').click(function(){
    $.getJSON("traitementReady.php",function(data){
        $("#categories").empty();
        $.each(data.result, function(){
            $("#categories").append(" <button id="+this['Id']+"  class='categorie'>"+this['Libelle']+"</button>");
        });
    });
});

$('.categorie').click(function(){
    alert("Yes");
});
</script>

$('#firstButton')。单击(函数(){
$.getJSON(“traitementrady.php”),函数(数据){
$(“#类别”).empty();
$.each(data.result,function(){
$(“#categories”).append(“+this['Libelle']+”);
});
});
});
$('.categorie')。单击(函数(){
警惕(“是”);
});
html:

OK
如果我单击按钮(确定),我拥有
div(id=categories)
中的所有值(按钮类(categorie)),但是如果我在加载了单击按钮(确定)的按钮中单击一个按钮,我什么也得不到(无警报(“是”)。

尝试这种方法

$(document).on('click','.categorie',function(){

alert("Yes");

 });

将click代码放在firstbutton click中,以便在创建后立即附加处理程序

$('#firstButton').bind('click',function(){
    ...
    $("#categories").empty();
    ...
    $('.categorie').off('click');//remove previous so as to avoid multiple bindings
   $('.categorie').on('click',function(e,o){
    alert("Yes");
  });
});

“on”、“bind”和“live”有一些细微的区别,如果您的HTML是动态创建的,那么您可以使用

jquery(“xxx”).live('click',function(){XXXXX})

Bind实际上会强制点击触发,但是html应该已经存在了

希望这会有所帮助


干杯

单击处理程序中的代码可能有问题。尝试放置一些断点并进行调试。
$('#firstButton').bind('click',function(){
    ...
    $("#categories").empty();
    ...
    $('.categorie').off('click');//remove previous so as to avoid multiple bindings
   $('.categorie').on('click',function(e,o){
    alert("Yes");
  });
});