weixin_33695450 2016-06-23 17:06 采纳率: 0%
浏览 20

AJAX两次加载内容

I am trying to build a sidebar navigation, which uses AJAX to load content inside a div. So far it is working great, it is just that i noticed, that everytime i click onto the buttons to navigate to a different site on my page, the site content seems to be loaded on top of the current content.

I have got this hypothesis, because on every site load my site sends one request to a server, to get data from there.

Now if i already visited the site on my page, that sends this request, navigate to a different site on my page and navigate back to the first site, this site sends two requests to the server. If i am repeating those steps, it just adds up and the request gets sent 3,4,5..times

This is the Code of my navigation bar:

<div id="sidedrawer" class="mui--no-user-select">
  <div class="mui-divider"></div>
  <ul>
    <li onClick="remote();" href="./#remote">
      <strong><a href="./#remote" class="navbarlink">Remote</a></strong>
    </li>
    <li onClick="groups();" href="./#groups">
        <strong><a href="./#groups"  class="navbarlink">Groups</a></strong>
    </li>
    <li onClick="devices();" href="./#devices">
      <strong><a href="./#devices"  class="navbarlink">Devices</a></strong>
    </li>
    <li onClick="users();" href="./#users">
      <strong><a href="./#users"  class="navbarlink">Users</a></strong>
    </li>
    <li onClick="programs();" href="./#programs">
      <strong><a href="./#programs"  class="navbarlink">Programs</a></strong>
    </li>
    <li onClick="settings();" href="./#settings">
      <strong><a href="./#settings"  class="navbarlink">Settings</a></strong>
    </li>
  </ul>
</div>

The functions remote()/groups()/devices() look about the same:

function remote() {
            showSubPage("Remote", "./remote.php");  
        }

showSubpage() looks like this:

function showSubPage(title, page){
    changeTabName(title);
    $.ajax({
        url: page,
        type: "GET",
        cache: false,
        success:function(data){
                                var $main = $('#main');
                                $main.html('');
                                $main.html(data);                               
                                }
    });

}

Main is just a normal div:

<div id="main"></div>

Does anyone have an idea why my page does not get called just once everytime i click the links?

Edit: When i am looking into the source code of my page, the content is only listed once.

Edit2: I figured that the problem does not come from the ajax itself. This is the PHP-code that i am using to generate my page.

function addGroup($groupId, $groupname, $devicearr)
    {
    echo('<div class="group">
            <h3>'.$groupname);
        echo('<div class="verticalcenter"><div class="toggle-button toggle-button-selected " id="groupButton'.$groupId.'"><button></button></div></div>');
    echo('      </h3>
            <div>
                <table class="devicetable">
                  ');

                  foreach ($devicearr as &$device) {
                        echo('<tr>
                                <td>'.$device['name'].'</td>');
                        if($device['status'] == 1){
                            echo('<td class="togglecolumn"><div class="verticalcenter" ><div class="toggle-button toggle-button-selected group'.$groupId.' device'.$device['id'].'" id="group'.$groupId.'device'.$device['id'].'" ><button></button></div></div></td></tr>');
                        } else {
                            echo('<td class="togglecolumn"><div class="verticalcenter"><div class="toggle-button group'.$groupId.' device'.$device['id'].'" id="group'.$groupId.'device'.$device['id'].'"><button></button></div></div></td></tr>');
                        }
                        echo ('<script>
                                        $(document).on("click","#group'.$groupId.'device'.$device['id'].'", function() {
                                            if($("#group'.$groupId.'device'.$device['id'].'").hasClass("toggle-button-selected")){
                                                $(".device'.$device['id'].'").removeClass("toggle-button-selected");
                                                    var frame = document.createElement("iframe");
                                                    frame.src = "http://localhost/callMethod";
                                                    frame.style.left = "-9999px";
                                                    frame.style.display = "none";
                                                    frame.onload = function() {
                                                        this.parentNode.removeChild(this);    
                                                    };
                                                    document.body.appendChild(frame);

                                            }
                                            else{
                                                $(".device'.$device['id'].'").addClass("toggle-button-selected");
                                                var frame = document.createElement("iframe");
                                                frame.src = "http://localhost/callMethod";
                                                frame.style.left = "-9999px";
                                                frame.style.display = "none";
                                                frame.onload = function() {
                                                    this.parentNode.removeChild(this);    
                                                };
                                                document.body.appendChild(frame);
                                            }            
                                        });

                                    </script>');
                    }
    echo('
                </table>
            </div>
          </div>');

}

My code creates a button for each "device" that is in the "devicearr". But when my page got called twice and i click the button once, it registers two click events.

  • 写回答

1条回答 默认 最新

  • weixin_33674437 2016-06-23 18:06
    关注

    Instead of:

     $(document).on("click","#group'.$groupId.'device'.$device['id'].'", function() {
    });
    

    I used:

    $("#group'.$groupId.'device'.$device['id'].'").click( function() {
    
    });
    

    so that the Click-Event does not stay alive after reloading the ajax content.

    Additionally (not necessarily needed), i also unbind the click-event, before i register the click-event with:

    $("#group'.$groupId.'device'.$device['id'].'").off("click");
    

    Thanks to Barmar for the hint.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮