2012-06-13 41 views
0

可能重複:
How-to store variable beetween jQM pages?如何使用Jquery mobile將數據從一個頁面發送到另一個頁面?

我是新來的jQuery移動和有點混亂將數據發送到另一個頁面。在我的html文件中,它由listview組成。當我點擊列表時,它需要重定向到另一個頁面,同時我想將該列表中的數據發送到第二頁。 其實我的代碼..

Polls.html

 <body> 
    <section id="pollsscreen" data-role="page"> 
     <header data-role="header" > 
      <h1> Video </h1> 
      <a href="index.html" data-role="button" data-icon="back" data-direction="reverse" data-transition="slide"> Back </a> 
     </header> 
     <div data-role="content"> 
      <ul id="pollslist" data-role="listview" data-theme="e" > 
      </ul> 
     </div> 
    </section> 
</body> 

Polls.js(我的JavaScript文件)

var addPollsList = function addPollsList() { 
    $("#pollslist").prepend("<li><a href='pollsdetails.html'>What is your name?</a></li>"); 
    $("#pollslist").prepend("<li><a href='pollsdetails.html'>Give review of new movie..</a></li>"); 
    $("#pollslist").listview("refresh");  
} 

pollsdetails.html

<section id="pollsdetailspage" data-role="page"> 
     <header data-role="header"> 
      <h1> Polls </h1> 
      <a href="polls.html" data-role="button" data-icon="back" data-direction="reverse" data-transition="slide"> Back </a>    
     </header> 
     <div data-role="content" class="pollsdetailsscreen"> 
      <p>Polls details page</p> 
     </div> 
    </section>  
</body> 
根據代碼,一切工作都很好。當我點擊列表時,它將重定向到 「pollsdetails.html」文件。但在這裏我不知道如何將數據發送到該網頁,我想在pollsdetails.html頁面將該數據設置爲

標籤。誰能幫我這個.......

在此先感謝...

回答

1

可以使用全局變量的..

在Polls.js

var addPollsList = function addPollsList() { 
$("#pollslist").prepend('<li><a href="#" onclick="test('+Ur_data+')">What is your name?</a></li>'); 
$("#pollslist").prepend('<li><a href="#" onclick="test2('+Ur_data+')">Give review of new movie..</a></li>'); 
$("#pollslist").listview("refresh");  

}

function test1(data){ 
    // set urs global variable here 
    global_variable = data; 
    $.mobile.changePage("pollsdetails.html"); 
} 

function test2(data){ 
    // set urs global variable 2 here 
    global_variable_2 = data; 
    $.mobile.changePage("pollsdetails.html"); 
} 

和pollsdetails.js您可以訪問global_variableglobal_variable_2

1

如果您嘗試將簡單的名稱/值對傳遞到下一頁,則可以使用查詢字符串參數。

var addPollsList = function addPollsList() { 
    $("#pollslist").prepend("<li><a href='pollsdetails.html?myparam=value1'>What is your name?</a></li>"); 
    $("#pollslist").prepend("<li><a href='pollsdetails.html?myparam=value2'>Give review of new movie..</a></li>"); 
    $("#pollslist").listview("refresh");  
} 
1

我會localStorage的

var userReview = { 
       "Name":"Bob", 
       "Review":"Awesome party movie!!", 
       "MovieTitle":"Project X" 
       }; 

      //Store the object in local storage 
      localStorage.setItem('UserReview',JSON.stringify(userReview)); 

,那麼你可以隨時撥打localStorage的,當你導航到不同的頁面以獲取信息回來。

var userReview = JSON.parse(localStorage.getItem("UserReview")); 
var userName = userReview.Name; 
var review = userReview.Review; 
var movieTitle = userReview.MovieTitle; 
相關問題