Jquery 按钮单击事件未触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19237235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:30:17  来源:igfitidea点击:

Jquery button click event not firing

javascriptjqueryhtmlajax

提问by GrepGrep

seems like a simple issue but the solutions to the other problem don't seem to work from me.

似乎是一个简单的问题,但另一个问题的解决方案似乎对我不起作用。

Trying to trigger a AJAX request from a button click but it doesn't seem to be firing.

试图通过单击按钮触发 AJAX 请求,但它似乎没有触发。

example HTML

示例 HTML

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

javascript

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;
});

回答by Lloyd Banks

The code you have works fine in fiddle. Is your button being dynamically rendered through AJAX after the initial page load?

您拥有的代码在小提琴中运行良好。您的按钮是否在初始页面加载后通过 AJAX 动态呈现?

Use

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

instead of

代替

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