正常运行时 jQuery 代码不起作用。但我确实激活了断点

发布于 2024-08-29 10:36:09 字数 371 浏览 4 评论 0原文

我的应用程序中有以下代码:

$(".deleteproduct").click(function() {
    id = this.id;
    alert("id: " + id);
});

如果我运行我的网站,当我单击具有类 deleteproduct 的元素时,不会发生任何情况。但是当我在第 1,2 和 3 行之前放置断点时,它就像一个魅力。

具有类deleteproduct的元素通过$(document).ready(function()后面的AJAX调用加载。.click代码位于其下面。

这对我来说真的很奇怪。如果使用Firebug中的断点,它会起作用,但如果使用Firebug中的断点,则不起作用没有...

I have the following code in my application:

$(".deleteproduct").click(function() {
    id = this.id;
    alert("id: " + id);
});

If i run my site nothing happens when I click on an element with the class deleteproduct. But when I place breakpoints before line 1,2 and 3 it works like a charm.

The elements with class deleteproduct are loaded through AJAX call right behind $(document).ready(function(). The .click code is underneath it.

This seems really weird to me. It's working if breakpoints in Firebug are used, but not when there are not...

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

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

发布评论

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

评论(3

与他有关 2024-09-05 10:36:09

如果您通过 Ajax 加载 .deleteproduct 元素,则可能需要使用 jQuery 的 live 事件处理程序。

$(".deleteproduct").live('click', function(){
    id = this.id;
    alert("id: "+id);
});

或者将上面的函数放在 Ajax 回调中。

If you are loading your .deleteproduct element via Ajax, you might need to define the click function using jQuery's live event handler.

$(".deleteproduct").live('click', function(){
    id = this.id;
    alert("id: "+id);
});

or put the function above in the Ajax callback.

梦开始←不甜 2024-09-05 10:36:09

对我来说听起来像是一个竞争条件。
您是否记得将该代码放入其中

$().ready(function(){
    ....
});

,以便在尝试针对 DOM 编写脚本之前文档已完全加载。

Sounds like a race condition to me.
Have you remembered to put that code inside

$().ready(function(){
    ....
});

That is so that the document has completely loaded before you try to script against the DOM.

苦妄 2024-09-05 10:36:09
$(document).ready(function(){
    $(".deleteproduct").click(function(){
        var id = $(this).attr('id');
        alert("id: "+id);
    });
});
$(document).ready(function(){
    $(".deleteproduct").click(function(){
        var id = $(this).attr('id');
        alert("id: "+id);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文