动态页面中JQuery被触发两次

发布于 2024-11-30 02:04:26 字数 2088 浏览 1 评论 0原文

我的问题:我有一个页面index.php,它动态地将一些html内容从admin_agent_area.php加载到index.php中的一些div(#adminarea)中。当单击链接“#adduser”并且 jquery 函数正在处理加载和插入时会发生这种情况。现在,在 admin_agent_area.php 中。当 admin_agent_area.php 加载时,我还有一些其他 div (#usertable) 加载了一个表。

id=#adduser 是index.php 中“adminarea”之外的链接()。

单击表格时会出现警报。

加载内容后,链接 #adduser 仍然可见。再次按下时,admin_agent_area.php 会再次加载,表格也会加载。现在,在表内单击会相继显示警报两次。这很糟糕,应该只有一次。我可以在 Firebug 中看到,当我希望事情只发生一次时,事情确实发生了两次。

我遇到过类似的问题,并在加载内容之前使用 .empty 解决了它。但这样做时,一切都在同一边。

index.php 结构

< a id="adduser"> Add user < /a>

< div id = "adminarea">< /div>

admin_agent_area.php 结构一部分

< div id="modalarea">< /div>

< div id="usertable">< /div>

admin_agent_area.php 我的javascript加载了index.php

$(document).ready(function() { 
      $("#adduser").click(function () {
                $("#adminarea").empty();
                    $("#adminarea").load("http://localhost/SMICAdmin/adminactivities/admin_agent_area.php", function(data) { }); 

            });
});

admin_agent_area.php中的所有内容都加载到index.php中的div #adminarea中。我认为在加载新内容之前确保我将其清空,可以这么说,重置整个#adminarea,就像我以前从未加载过任何东西一样。

这是与 admin_agent_area.php 一起加载的 javascript。当单击表中的一行时,会触发此脚本:

$("table[id$='agents'] td:nth-child(1)").live('click',function(event)  

        {  
            $("#usertable").empty();
            alert("loadupdatagent");

            event.preventDefault();  

            var $td= $(this).closest('tr').children('td');  

            var $agentid=$td.eq(2).text();  

        $.get("http://localhost/SMICAdmin/adminactivities/admin_update_agent.php", { agent_id: $agentid }, function(data){ 
            $("#modalarea").empty();
            $("#modalarea").html(data);
            $('#modalarea').css("visibility","visible");
        });

正如您所看到的,这是警报所在的脚本。

id=#modalarea 是单击表格行后应该加载某些内容的区域。对于问题本身来说应该不重要,但我可以在 Firebug 中看到 .get 请求也发出了两次。

我该如何解决这个问题?我认为不需要 HTML 代码,请告诉我,我也会添加它。

My problem: I have a page, index.php, that dynamically loads some html contents, from admin_agent_area.php, into some divs (#adminarea) in index.php. This happens when clicking the link "#adduser" and a jquery function is handling the loading and inserting. Now, in admin_agent_area.php. I have some other divs (#usertable) that is loaded with a table when the admin_agent_area.php has been loaded.

id=#adduser is a link (< a >) in index.php, outside "adminarea".

When clicking in the table an alert is presented.

The link #adduser is still visible after content has been loaded. When pressing it again the admin_agent_area.php is once again loaded and the table as well. Clicking inside the table now presents the alert two times after each other. This is bad, it should be only one time. I can see in Firebug that things are indeed happening twice when I want it to happen only once.

I have had similar problem and solved it by using .empty before loading the content. But when doing that, everything was on the same side.

index.php part of structure

< a id="adduser"> Add user < /a>

< div id = "adminarea">< /div>

admin_agent_area.php part of structure

< div id="modalarea">< /div>

< div id="usertable">< /div>

admin_agent_area.php
My javascript loaded with index.php

$(document).ready(function() { 
      $("#adduser").click(function () {
                $("#adminarea").empty();
                    $("#adminarea").load("http://localhost/SMICAdmin/adminactivities/admin_agent_area.php", function(data) { }); 

            });
});

All content from admin_agent_area.php is loaded into div #adminarea in index.php. I thought that making sure i .empty it before loading the new content would, so to speak, reset entire #adminarea, like if I never had loaded anything before.

This is the javascript loaded together with admin_agent_area.php. When clicking a row in the table, this script is triggered:

$("table[id$='agents'] td:nth-child(1)").live('click',function(event)  

        {  
            $("#usertable").empty();
            alert("loadupdatagent");

            event.preventDefault();  

            var $td= $(this).closest('tr').children('td');  

            var $agentid=$td.eq(2).text();  

        $.get("http://localhost/SMICAdmin/adminactivities/admin_update_agent.php", { agent_id: $agentid }, function(data){ 
            $("#modalarea").empty();
            $("#modalarea").html(data);
            $('#modalarea').css("visibility","visible");
        });

And as you can see, this is the script where the alert is.

id=#modalarea is the area where some content is supposed to be loaded once the table row has been clicked. It shouldn't matter for the problem itself, but I can see in Firebug that the .get request is made twice as well.

How do I fix this problem? I don't think the HTML code should be needed, please let me know and I will add it as well.

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

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

发布评论

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

评论(5

您的好友蓝忘机已上羡 2024-12-07 02:04:26

试试这个:

$("table[id$='agents'] td:nth-child(1)").live('click',function(event) {
    if($(this).is('#adduser'))
    {
        $("#adminarea").empty();
        $("#adminarea").load("http://localhost/SMICAdmin/adminactivities/admin_agent_area.php", function(data) { }); 
    }
    else
    {
        // rest of function....
    }
});

Try this:

$("table[id$='agents'] td:nth-child(1)").live('click',function(event) {
    if($(this).is('#adduser'))
    {
        $("#adminarea").empty();
        $("#adminarea").load("http://localhost/SMICAdmin/adminactivities/admin_agent_area.php", function(data) { }); 
    }
    else
    {
        // rest of function....
    }
});
垂暮老矣 2024-12-07 02:04:26

尝试使用 remove(true) 而不是 empty()。这实际上就像 HTMLElement 从未出现在页面上一样。 true 意味着所有事件处理程序也将被取消注册。

Instead of empty() try using remove(true). That will effectively act as if the HTMLElement was never on the page. The true means that all event handlers will be unregistered as well.

温折酒 2024-12-07 02:04:26

好吧,这(对我来说)是一个凝灰岩。

当加载 admin_agent_area.php 时,它也会加载 .js 文件,因为 <<脚本...>标签是在该文件中定义的。多次单击“#adduser”时,admin_agent_area.php 和 .js 文件也会加载多次。

我不明白事件处理程序如何绑定到元素,但在某种程度上,每次加载时我都会为我的元素获得更多事件处理程序。至少看起来是这样。

我的解决方案是动态地从index.php加载.js文件。由于某种原因,它无法在 << 中加载 .js 文件。脚本......>在index.php 中,脚本对于动态加载的内容似乎不可见。如果您知道这一点,请毫不犹豫地解释一下。但我想如果元素不可用于绑定事件处理程序,则不可能首先加载 .js 文件。

 $(document).ready(function() {
      var register_agent_page;   
      $("body").delegate("#adduser", "click", function (event) {                
                        if(register_agent_page==null){ 
                            $("#adminarea").load("http://localhost/SMICAdmin/adminactivities/admin_agent_area.php", function(data) {
                                    register_agent_page = data;
                                    $.getScript("http://localhost/SMICAdmin/adminactivities/admin_agent_script.js");
                                    $('#usertable').load("http://localhost/SMICAdmin/adminactivities/admin_load_agents.php");                                             

                            }); 
                        }else{

                            $("#adminarea").html(register_agent_page);                         
                            $('#usertable').load("http://localhost/SMICAdmin/adminactivities/admin_load_agents.php");                                             
                        }
  });

我确保只在第一次加载页面,不知道这是否重要,但它节省了一些请求。我第一次单击“#adduser”时也会加载 .js 文件。现在,.js 文件将仅被调用一次。

无法正常工作的是在加载 .js 文件时自动将数据加载到用户“#usertable”中。第一次很好,但第二次我点击“#adduser”时,.js 文件已经加载,我需要以不同的方式加载用户表。因此,我加载该表并将其添加到由index.php 加载的javascript 文件中的div“#usertable”。

噗!

Ok, this was (for me) a tuff one.

When loading admin_agent_area.php it will load the .js file as well since the < script...> tag was defined in that file. When clicking "#adduser" several times the admin_agent_area.php and, hence, the .js file is loaded several times as well.

I don't understand how event handlers are binded to elements, but in some way, each time I load I do get more event handlers for my elements. At least that's how it looks like.

The solution for me was to load the .js file from index.php dynamically. Of some reason it doesn't work loading the .js file in the < script....> of index.php, the scripts doesn't seem to be visible for the dynamically loaded content. Don't hesitate to explain this if you know about it. But I guess it's not possible to first load a .js file if the elements are not available to bind event handlers to.

 $(document).ready(function() {
      var register_agent_page;   
      $("body").delegate("#adduser", "click", function (event) {                
                        if(register_agent_page==null){ 
                            $("#adminarea").load("http://localhost/SMICAdmin/adminactivities/admin_agent_area.php", function(data) {
                                    register_agent_page = data;
                                    $.getScript("http://localhost/SMICAdmin/adminactivities/admin_agent_script.js");
                                    $('#usertable').load("http://localhost/SMICAdmin/adminactivities/admin_load_agents.php");                                             

                            }); 
                        }else{

                            $("#adminarea").html(register_agent_page);                         
                            $('#usertable').load("http://localhost/SMICAdmin/adminactivities/admin_load_agents.php");                                             
                        }
  });

I am making sure that I only load the page the first time, don't know if it matters but it saves some requests. The .js file is loaded the first time I am clicking "#adduser" as well. Now, the .js file will be called only once.

What doesn't work properly is to automatically load the data into user "#usertable" from the .js file when it has been loaded. First time is fine, but second time I hit "#adduser" the .js file is already loaded and I need to load the user table in a different way. Hence, I load the table and add it to div "#usertable" from within the javascript file loaded by index.php.

Puh!

南街九尾狐 2024-12-07 02:04:26

嘿,关于动态页面无法访问脚本的说法是不正确的。

事件处理程序是在脚本加载时注册的,因此它不会注册那些动态创建的元素。解决方案是使用 jquery 的“on”函数,即使在加载脚本后也可以绑定元素。

Hey regarding the script not being accessible to dynamic pages is not true.

Event handlers are registered at the load of the script and so it is not registering those elements created dynamically. The solution for this using 'on' function of jquery which allows you to bind elements even after the script has been loaded.

吝吻 2024-12-07 02:04:26

@Nicsoft

如果有人读过我的评论jquery在dom加载后不会添加事件处理程序,所以你需要使用'on'函数,委托,live函数是较旧的函数,很快就会被删除。 'on' 是为动态加载的元素添加事件处理程序的替代方法。

http://api.jquery.com/on/

这是 on 命令的链接

$( " < 任何不像 body 那样动态加载的元素 > ").on(' < 事件名称 > ', ' < 动态加载的实际元素 > ', function(){
// 您想要为事件执行的代码
});

@Nicsoft

If anyone has read my comment jquery doesn't add event handlers after the dom is loaded so u need to use 'on' function, delegate,live function is the older one and is going to removed soon. 'on' is the replacement to add event handlers for dynamically loaded elements.

http://api.jquery.com/on/

this is the link to the on command

$(" < any element that doesn't load dynamically like body > ").on(' < event name > ', ' < actual element loading dynamically > ', function(){
// code you want to perform for the event
});

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