2016-12-03 13 views
0

jqueryのボタンイベント内の変数をリセットしようとすると問題が発生します。この変数はdivに読み込まれたページを表します。次に、setInterval関数を使用して、変数に格納されたページでdiv要素をリフレッシュします。 これはコードです:関数内で変数を更新するJQuery

$(document).ready(function() { 
    var current_page="data.php"; //For update-the first page shown is data.php 

    //At startup 
    $("#content").load("/data.php",function(response){ 
     //current_page="data.php"; The first command-current_page already set to "data.php" 
     if(response=="0"){ 
      location.href="http://website.com"; 
     } 
    }); 

    //JS refresh timer 
    setInterval(function(){ 
     alert(current_page); //Always shows 'data.php'->means it is not updated 

     if(current_page!="insert.php"){ 
      $("#content").load("/"+current_page,function(response){ 
       if(response=="0"){ 
        location.href="http://website.com"; 
       } 
      }); 
     } 
    },5000); //5 seconds 


    //EVENTS 

    //Menu links pressed 
    $(".menu_link").click(function(){ 
     var page=$(this).attr("data-page"); 
     current_page=page;  //Here it works. 

     $("#content").load("/"+page,function(response){ 
      if(response=="0"){ 
       location.href="http://website.com"; 
      } 
     }); 
    }); 
}); 

//Outside Jquery 'document' 
    //Select user button pressed 
    function loadData(){ 
    var user_id=$("#menu_selector option:selected").val(); 
    current_page="users.php?user_id="+user_id;  //Not globally updated;inside function it is set. 

    $("#content").load("/users.php?user_id="+user_id,function(response){ 
     if(response=="0"){ 
      location.href="http://website.com"; 
     } 
    }); 

}

私は私のコードで「警告」の文を入れてCURRENT_PAGEの値をテストしました。結論:setInterval関数のcurrent_page変数は常に "data.php"に設定されています。最初の行を削除した場合var current_page="data.php";current_pageは '未定義'です。 loadData関数によって更新されていないようです。
また、loadData関数をJQueryの読み込み内に移動してみましたが、ボタンが見つからない(<button onclick="loadData();">Load page</button>を使用)

問題はどこですか?

+0

は思わ:だからではなく、名前空間を作成するか、他の私はjqueryのコード外の宣言を移動しましたあなたのスクリプトの先頭に... – RohitS

+0

はい..私はすでにそれを修正しました。私はその答えを見ました。変数がjqueryの部分だけにグローバルではないことは分かっていました。 –

答えて

1

現在のページ変数の範囲内で機能が有効になります。 loaddataのにある機能外線:

current_page="users.php?user_id="+user_id; 

は、実際にはまだCURRENT_PAGEと呼ばれる別の変数を設定するが、ウィンドウ対象になります。あなたはChromeの開発ツールでこれを見ることができるはずです。

$()関数の外側からこれを操作できるようにする必要がある場合は、より多くのグローバルスコープで宣言する必要があります(たとえば、ウィンドウオブジェクトに対して)。

0

私はそれを解決しました。 マークウィリアムズは変数が「十分にグローバル」ではないと書きました。あなたは...()関数をdocument.readyグローバルスコープで外で宣言しようとするあなたの変数をスコープしているように、右...

var current_page="data.php"; //Moved outside 
$(document).ready(function() { 
    //At startup 
    $("#content").load("/data.php",function(response){ 
     //current_page="data.php"; The first command-current_page already set to "data.php" 
     if(response=="0"){ 
      location.href="http://website.com"; 
     } 
    }); 

    //JS refresh timer 
    setInterval(function(){ 
     alert(current_page); //Always shows 'data.php'->means it is not updated 

     if(current_page!="insert.php"){ 
      $("#content").load("/"+current_page,function(response){ 
       if(response=="0"){ 
        location.href="http://website.com"; 
       } 
      }); 
     } 
    },5000); //5 seconds 


    //EVENTS 

    //Menu links pressed 
    $(".menu_link").click(function(){ 
     var page=$(this).attr("data-page"); 
     current_page=page;  //Here it works. 

     $("#content").load("/"+page,function(response){ 
      if(response=="0"){ 
       location.href="http://website.com"; 
      } 
     }); 
    }); 
}); 

//Outside Jquery 'document' 
    //Select user button pressed 
    function loadData(){ 
    var user_id=$("#menu_selector option:selected").val(); 
    current_page="users.php?user_id="+user_id;  //Not globally updated;inside function it is set. 

    $("#content").load("/users.php?user_id="+user_id,function(response){ 
     if(response=="0"){ 
      location.href="http://website.com"; 
     } 
    }); 
関連する問題