jQuery Moblie: passing parameters and dynamically load the content of a page

折月煮酒 提交于 2019-12-13 04:58:50

问题


I know it's a base problem, but I wasn't able to find anything fitting my situation.

I have a jQuery Mobile page with a list of elements. The elements references to a second pages which has a content that depends on what element where choose (for example a list of articles and choosing an article it should show the relative text). Basically I have to pass a parameter between pages.

I tried to do that using AJAX:

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

But i wasn't able to get the results in the second page.

How can I do that?


回答1:


By the look of your URL: index.html#page2. You are trying to navigate to an internal page (one already in the DOM). If this is the case, then make the logic in JavaSCript on #page2 and when you link to #page2, save the id value in a variable that the JavaScript on #page2 can access.

Something like:

<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>

Here is a demo: http://jsfiddle.net/jasper/wFdet/



来源:https://stackoverflow.com/questions/11296156/jquery-moblie-passing-parameters-and-dynamically-load-the-content-of-a-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!