dongyang4615 2013-07-26 10:47
浏览 47
已采纳

jQuery .show()在刷新之前无法工作

I have a jQuery function to display various things depending on whether a user was logged in or not.

The function is in the header which is included in every page (I tried it within the head of the page in question with the same result.

The id loggedIn is set to display:none and the the jquery should display it once logged in but it never does.

I've tested with both loggedIn and loggedOut but nether appear, however the head does update the links, albeit after a refresh and that's the other issue.
In the header, I have to refresh the page before the links change.

Thanks tons for any help.

<script>
$(document).ready(function () {
    var loggedIn = <? php echo json_encode($general - > loggedIn()); ?> ;
    if (loggedIn == true) {
        $("#loggedIn").show();
    } else {
        $("#loggedOut").show();
    }
});            
</script>

Neither of the loggedIn or loggedOut elements display in the below code.

<p id="loggedOut">YOOOOHOOOOOOO</p>
  </div>

<?php

 $body = <<<EOT
        <div class="viewAndDownload" id="loggedIn">
            <a href="../download.php?filename=bla.pdf">Click to download the PDF/>
</div>
EOT;

This is the HTML from the header where the login box doesn't show until after a refresh:

<nav class = "memberHeaderArea" id="loggedIn">
    Links Here
</nav>

<nav class = "memberHeaderArea" id ="loggedOut">
 Removed links etc to save space
<input type="submit" name="loginButton" id="loginButton" value="Login!"   onclick="validLogin(); return false;"/>

onclick calls an ajax function

function validLogin(){

//removed get username etc

var params = {username: username, password: password};
var url = "../loginProcessAjax.php";

$("#statusLogin").show();

$.ajax({
    type: 'POST',
    url: url,
    data: params,
    dataType: 'json',
    beforeSend: function() {
      document.getElementById("statusLogin").innerHTML= '<img src="../images/loginLoading.gif" /> checking...' ;
    },

    success: function(data) {

        $("#statusLogin").hide();

        if(data.success == true){

            //$('#loggedIn').show();
            $('#loginContent').slideToggle();
            //$('#loggedOut').hide();

        }else{
           // alert("data.message... " + data.message);//undefined
            $("#error").show().html(data.message);
        }

    },
    error: function( error ) {
        console.log(error);
    }
});
}

Thank you again, this has been bugging me for ages and I haven't found a way to solve it despite many attempts

EDIT: logged in function

public function loggedIn(){

return (isset($_SESSION['id'])) ? true : false;
}//end function
  • 写回答

2条回答 默认 最新

  • douli1306 2013-07-26 11:16
    关注
    $(document).ready(function(){
        function testLoggedIn(){
          var loggedIn = <? php echo json_encode($general - > loggedIn()); ?> ;
            if (loggedIn == true) {
              $("#loggedIn").show();
            } else {
              $("#loggedOut").show();
            }
        }
    
        setInterval(testLoggedIn(),1000);
    });
    

    Something like this should work.

    EDIT The main idea is you have to constantly check if the user is logged in or not and not only on document ready. To achieve that you can use setInterval which will keep executing the function according to the interval value passed in parameters. This way , if this script is running on all your pages, all pages of the user will update automatically if he logs in / logs out, and not only the page where he fired the login event.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号