0

可能重复:
如何在 jQM 页面之间存储变量?

我是 jquery mobile 的新手,对将数据发送到另一个页面有点困惑。在我的 html 文件中,它由 listview 组成。当我单击列表时,它需要重定向到另一个页面,同时我想将该列表中的数据发送到第二页。其实我的代码是..

民意调查.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”文件。但是在这里我不知道如何将数据发送到该 html 页面,我想将该数据设置为

pollsdetails.html 页面中的标记。谁能帮我这个.......

提前致谢...

4

3 回答 3

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.

于 2012-06-13T08:47:28.540 回答
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;
于 2012-06-13T15:19:22.150 回答
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");      
}
于 2012-06-13T15:04:00.317 回答