Jquery按钮单击事件未触发

时间:2013-10-08 00:43:20

标签: javascript jquery html ajax

似乎是一个简单的问题,但其他问题的解决方案似乎并不适用于我。

尝试通过点击按钮触发AJAX请求,但似乎没有触发。

示例HTML

<button class="remove_weight_button" id="15">x</button>

的javascript

$(".remove_weight_button").click(function(){
    var button_id = $(this).attr("id");
    $.ajax({
        type: "POST",
        url: "weight_tracker_process.php",
        data: {
            weight_id: button_id,
            action: "remove"
        },
        success: function(){
            getWeightData();
        },
        error: function(){
            alert("data removal error");
        }
    });
    return false;
});

1 个答案:

答案 0 :(得分:95)

你的代码在小提琴中工作得很好。在初始页面加载后,您的按钮是通过AJAX动态呈现的吗?

使用

$(document).on("click", ".remove_weight_button", function(){

而不是

$(".remove_weight_button").click(function(){