0

我知道这是一个基本问题,但我找不到适合我情况的任何东西。

我有一个带有元素列表的 jQuery Mobile 页面。元素引用第二个页面,该页面的内容取决于选择的元素(例如文章列表和选择应该显示相关文本的文章)。基本上我必须在页面之间传递一个参数。

我尝试使用 AJAX 来做到这一点:

$.ajax({
    url : "index.html#page2",
    data : "id="+id,    
    dataType : 'json'
    type: "GET"
});

但是我无法在第二页中获得结果。

我怎样才能做到这一点?

4

1 回答 1

1

从您的网址看:index.html#page2. 您正在尝试导航到一个内部页面(一个已经在 DOM 中的页面)。如果是这种情况,则将 JavaSCript 中的逻辑设置为 on #page2,当您链接到 时#page2,将值保存在 JavaScript on可以访问id的变量中。#page2

就像是:

<ul data-role="listview" id="myList">
    <li>
        <a href="#page2" data-id="987">This is some fake text... Click for More</a>
    </li>
</ul>
<script>
$(document).delegate('#page1', 'pageinit', function () {

    //bind to click event for all links in the `#myList` UL
    $(this).find('#myList').find('a').bind('click', function () {

        //save the ID of this list-item to a global variable so it can be used later
        window.myId = $(this).attr('data-id');
    });
});
$(document).delegate('#page2', 'pageshow', function () {

    //get the ID saved as a global variable
    var currentId = window.myId;

    //now do logic for second page
});
</script>

这是一个演示:http: //jsfiddle.net/jasper/wFdet/

于 2012-07-02T16:05:35.003 回答