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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)