jQuery 无法使用 .on() 将事件绑定到 AJAX 加载的元素

发布于 2024-12-19 09:50:37 字数 1261 浏览 0 评论 0原文

使用 .load,我将以下内容加载到 #step_2 div 中:

<div id="step_2">
    Select a subcategory:
    <select id="subcategory_id" name="subcategory_id">
        <option value="0">All subcategories</option>
        <option value="68">Cool</option>
        <option value="15">Cooler</option>
        <option value="74">Excellent</option>
    </select>
</div>

然后我尝试检测用户何时选择某些内容:

$("#subcategory_id").on("change", function(){
    alert( 'Success!' );
});

但没有成功。有人看到我做错了什么吗?非常感谢帮助。

注意:

这是 FireBug 看到的完整 CSS 路径:

html.js body div#container div#main form div#step_2 select#subcategory_id

如果上下文很重要,这是完整的 Javascript 文件:

$(document).ready(function() {

    $("#category_id").change(function() {
        $("#spinner").show();
        $("#step_2").load("process.php?selected=category", { value: $(this).val() }, function() {
            // Do something to say the data has loaded sucessfully
            $("#spinner").hide();
        });
    });
            
    $("#subcategory_id").on("change", function(){
        alert( 'Success!' );
    });
    
});

Using .load, I'm loading the following content into #step_2 div:

<div id="step_2">
    Select a subcategory:
    <select id="subcategory_id" name="subcategory_id">
        <option value="0">All subcategories</option>
        <option value="68">Cool</option>
        <option value="15">Cooler</option>
        <option value="74">Excellent</option>
    </select>
</div>

I'm then trying to detect when user selects something:

$("#subcategory_id").on("change", function(){
    alert( 'Success!' );
});

but with no success. Does anyone see what I'm doing wrong? Help greatly appreciated.

NOTES:

Here's the full CSS path as seen by FireBug:

html.js body div#container div#main form div#step_2 select#subcategory_id

Here's the full Javascript file, if context matters:

$(document).ready(function() {

    $("#category_id").change(function() {
        $("#spinner").show();
        $("#step_2").load("process.php?selected=category", { value: $(this).val() }, function() {
            // Do something to say the data has loaded sucessfully
            $("#spinner").hide();
        });
    });
            
    $("#subcategory_id").on("change", function(){
        alert( 'Success!' );
    });
    
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝戈者 2024-12-26 09:50:37

在这种情况下,您需要以与delegate类似的方式使用on,您使用的语法是bind的替代。

$("#step_2").on("change", "#subcategory_id", function(){
    alert( 'Success!' );
});

第一个选择器必须是不会被替换的元素。这样,当事件冒泡到这一点时,它就会被捕获。然后,您将触发事件的实际元素指定为 .on 的第二个参数。

In this case you need to use on in a similar way to delegate, the syntax you are using is the replacement for bind.

$("#step_2").on("change", "#subcategory_id", function(){
    alert( 'Success!' );
});

The first selector has to be an element that won't be replaced. This way, when the event bubbles up to this point it is caught. You then specify the actual element that will trigger the event as the 2nd parameter of .on.

半城柳色半声笛 2024-12-26 09:50:37

如果没有正确的设置,on(...) 不会订阅页面上尚未出现的事件。要执行您想要的操作,您应该尝试以下操作:

$(document).on("change", "#subcategory_id", function(){
    alert( 'Success!' );
});

为了提高效率,您应该将 $(document) 替换为您知道在执行此代码时将出现在页面上的元素的最接近的父元素被称为。

on(...) won't subscribe to events that aren't on the page yet without the proper setup. To do what you want, you should try the following:

$(document).on("change", "#subcategory_id", function(){
    alert( 'Success!' );
});

To make this more efficient, you should replace $(document) with the closest parent of the element that you know will be on the page when this code is called.

红焚 2024-12-26 09:50:37

您必须在加载 html 后绑定处理程序,或者使用

$("#step_2").on("change", "#subcategory_id", function(){
    alert( 'Success!' );
});

You have to bind the handler after you've loaded the html, or use

$("#step_2").on("change", "#subcategory_id", function(){
    alert( 'Success!' );
});
恏ㄋ傷疤忘ㄋ疼 2024-12-26 09:50:37

当然可以,

$(document).on("change", "#subcategory_id", function(){...});

要使用 .on 委托事件,请提供要绑定的父元素,然后提供要委托的选择器。

Sure it can,

$(document).on("change", "#subcategory_id", function(){...});

To delegate events using .on, provide a parent element to bind to, and then the selector to delegate to.

孤芳又自赏 2024-12-26 09:50:37

您必须在加载新元素后设置事件。由于 AJAX 是异步的,因此您需要在加载中具有回调。

$(document).ready(function() {

    $("#category_id").change(function() {
        $("#spinner").show();
        $("#step_2").load("process.php?selected=category", { value: $(this).val() }, 
        function() {
        // Do something to say the data has loaded sucessfully
            $("#spinner").hide();

            $("#subcategory_id").on("change", function(){
                alert( 'Success!' );
            });

        });
    });

});

You have to set the event after that you have loaded the new element. As AJAX is asynchronous, you need to have a callback in your load.

$(document).ready(function() {

    $("#category_id").change(function() {
        $("#spinner").show();
        $("#step_2").load("process.php?selected=category", { value: $(this).val() }, 
        function() {
        // Do something to say the data has loaded sucessfully
            $("#spinner").hide();

            $("#subcategory_id").on("change", function(){
                alert( 'Success!' );
            });

        });
    });

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文