将参数传递给jQuery Mobile中的页面

时间:2012-09-05 21:26:21

标签: javascript jquery jquery-mobile

我有两个页面,p1.htmlp2.html与jQuery Mobile。

p1.html中有一些链接,所有链接都导航到p2.html,但差别不大。

我想将int参数传递给p2.html javascript(例如,链接号),因此p2.html中的javascript代码会根据{{{{}}对其自身进行少量更改1}}参数(转换开始前)。

我想使用Ajax导航功能和jQuery Mobile的Transitions,而且我不想使用页面锚点(int)。

如何将该参数发送到p2.html#6

3 个答案:

答案 0 :(得分:2)

您可以使用cookies保存相关信息吗?

获取cookie jquery plugin

p1.html

<a href="p2.html" data-p2data="1">Link 1</a>
<a href="p2.html" data-p2data="2">Link 2</a>
<script type="text/javascript">
$(document).ready(function(){
    $('a[data-p2data]').click(function(){
        $.cookie('p2data', $(this).data('p2data'));
    }
}
</script>

p2.html

<script type="text/javascript">
    var p2data = $.cookie('p2data');
    // .. process the page based on the value in p2data.
</script>

答案 1 :(得分:1)

docs开始,没有内置的方法来在页面之间传递参数。

但是,您可以使用其中提到的其中一个插件(page-paramsjquery-mobile-router),也可以使用localStorage或Cookie。

看看其中一些SO问题

Passing data between pages with jQuery Mobile?

Passing parameters to a page-id in jQuery Mobile

How-to store variable beetween jQM pages?

答案 2 :(得分:1)

单击page1.html上的链接时,可以使用全局变量存储该数值:

$('a.special_link').click(function() {window.my_id = this.id});

...您将能够在page2.html中检索此内容,因为默认情况下JQM使用AJAXed导航,因此只能使用此变量保留在单个页面上:

$('document').on('pagebeforechange', function(toPage, data) {
    if (toPage == 'page2.html') {
        alert('you have clicked on link number: ' + window.my_id);
    }
});
相关问题