jQuery点击事件不能使用我的代码

时间:2016-08-10 05:44:58

标签: javascript jquery

这是我在jquery中的代码,我想获取tech-support-bullet-chart2中的数据

$.ajax({
    type:"POST",
    url:"includes/bullet-chart.php",
    data:{
        daterange:ym
    }
}).done(function(e){
    var tech_support = JSON.parse(e);
    //console.log(tech_support);
    for(i=0;i<t_s.length;i++){
    //console.log("support:"+tsn + ", complete:" + tsc + " ,Pending:"+ tsp +", isExist:"+isExist);
    $("#div-charts").append('<div class="tech-support-list">'+
    ''+
    '<div class="tech-support-name">'+t_s[i]+'(<label id="i'+t_s[i]+'"></label>)</div><div class="support-charts">'+
    '<div class="tech-support-bullet-chart1" id="'+t_s[i]+'-close">'+
        '</div><div class="spacer"></div>' +
        '<div class="tech-support-bullet-chart2" data-sname="'+t_s[i]+'" id="'+t_s[i]+'-open">' +
        '</div></div></div>');
    }

这是我在创建数据sname

时的功能
$(".tech-support-bullet-chart2").click(function(){
var name = $(this).data("sname");
alert(name);})

我的问题是..我的第二个代码不起作用..请帮我这个..

提前谢谢你!

1 个答案:

答案 0 :(得分:0)

更新您的代码,如:

$(document).on("click", ".tech-support-bullet-chart2", function(){
    var name = $(this).data("sname");
    alert(name);
})

这是将event处理程序绑定到DOM元素的事件委托技术,它们在加载文档后被添加到DOM中。