简体   繁体   English

Ajax可以更新动态导航菜单吗?

[英]Can Ajax update a dynamic navigation menu?

I'm using a PHP function to determine what links are being displayed: 我正在使用PHP函数来确定正在显示的链接:

<?php
  //amend toplinks if logged in
  if($general->loggedIn()){ ?>
    <nav class = "memberHeaderArea">
      <ul>
        <li>
          <a href="userProfile.php?username=<?php echo $user['username'];?>">Profile<span class="user icon"></span></a>
        </li>
        <li>
          <a href="userLogout.php">Log out<span class="lock icon"> </span></a>
        </li>
      </ul>
    </nav>
  <?php
    //toplinks for when not logged in
  }else{ ?>

I've just changed the structure of my login to use Ajax and it works perfectly with the exception of the nav. 我刚刚更改了登录结构以使用Ajax,并且除了nav之外,它都可以完美运行。 It still shows login / register until a page refresh is called. 它仍然显示登录/注册,直到调用页面刷新为止。 Obviously the reason for using Ajax was to avoid the page refresh, but is there a way I can update these links within the success function? 显然,使用Ajax的原因是为了避免刷新页面,但是有什么方法可以在成功函数中更新这些链接吗?

success: function(data) {
  $("#statusLogin").hide();
  if(data.success == true){
    $('#loginContent').slideToggle();
  }else{
    // alert("data.message... " + data.message);//undefined
    $("#error").show().html(data.message);
  }

From what I've found, it looks like I may need to assign the nav's id and then use Ajax / jQuery to target that. 从我发现的结果看,我可能需要分配导航的ID,然后使用Ajax / jQuery来定位该ID。 But I'm not sure how. 但是我不确定如何。 Any help greatly appreciated. 任何帮助,不胜感激。

This is a basic (and dummy) stuff for checking login status with AJAX after page loading. 这是基本的(也是虚拟的)东西,用于在页面加载后使用AJAX检查登录状态。 Probably you want more serious check then if(true) and an echo call, but it will be something like this :) Check the fiddle below, so you can see the html and the css too... 也许您想更认真地检查if(true)echo调用,但这将是这样的:)检查下面的小提琴,这样您也可以看到html和css ...

$.getJSON('/echo/json/').done(function (data) {
    console.log("Call returned");
    if (true){
        //successful login
        $('#loggedin').slideToggle();
    }else {
        //unsuccessful login
        $('#login').slideToggle();
    }
}).fail(function (data) {
    console.log("Call failed");
    $('#login,#error').slideToggle();
}).always(function (data) {
    console.log("Always running");    
});

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM