【问题标题】:jquery checkbox click function created with JSON not working使用 JSON 创建的 jquery 复选框单击功能不起作用
【发布时间】:2014-01-27 08:02:54
【问题描述】:

我有一个jquery-ajax 函数,它创建了一个复选框列表。

success: function(msg){

    jQuery.each(msg.gal_sublocations, function(index, gal_sublocation) {
        html += '<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "'+gal_sublocation.GalSublocation.id+'" class="chkbx" id="sublocation_'+gal_sublocation.GalSublocation.id+'" />'+gal_sublocation.GalSublocation.name;

        $('#load_sublocations').html(html);
       alert(html);
       $('#sublocation_'+gal_sublocation.GalSublocation.id).click(function(){
            alert('Checked !');
        });

    });  

} 

alert(html) 产生最终警报

<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "99" class="chkbx" id="sublocation_99" />Beltola
<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "101" class="chkbx" id="sublocation_101" />Dispur
<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "102" class="chkbx" id="sublocation_102" />Paltan Bazar
<input type="checkbox" name="data[GalStore][gal_sublocation_id][]" value = "100" class="chkbx" id="sublocation_100" />Patarkuchi

但是当我点击复选框时,它没有提醒任何东西!怎么了?

【问题讨论】:

    标签: jquery json checkbox cakephp-1.3


    【解决方案1】:

    您需要使用 jquery On() 因为您的代码在加载后追加。

    这样使用;

    $(".class").on('click', function(){
     // ajax operation
    }}
    

    【讨论】:

    • 嗨,Mehmet..它也不起作用!相同的 。只有最后一个复选框有效!
    【解决方案2】:

    在创建输入字符串时,最好在此处创建一个节点:

    var html = $("<input />").attr({
        "type" : "checkbox",
        "name" : data[GalStore][gal_sublocation_id][],
        "id" : 'sublocation_' + gal_sublocation.GalSublocation.id,
        "class" : "chkbx"
    })
    .val(gal_sublocation.GalSublocation.id)
    .click(function(){
          alert('Checked !');
    }); 
    
    $('#load_sublocations').append(html);
    

    【讨论】:

    • 嗨,Ashish,它不工作!它甚至没有填充复选框:(
    • 真的,在这里检查一下:jsfiddle.net/8wtJG/1 评论的事情是因为这些数据来自您的 ajax,在我的示例中,不可用。
    猜你喜欢
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多