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

我试图将一些参数传递给jQuery Mobile中的页面ID。

该网站由链接的列表视图组成,每个都有哈希编码,如下所示:

<li><a href="#pronostico?region=12&ciudad=0">Puerto Natales</a></li>

我已经绑定pagebeforechange在URL中捕获哈希,做参数检测,并根据传递的参数量采取行动。

现在,使用cookies,我一直在尝试:

$(document).one("pageinit",function(event,data) {
  if (location.hash.search(/^(#ciudades|#pronostico)/) === -1) {
    if ($.cookie("recordar")) {
      $.mobile.changePage($("#pronostico"),{
        data: "region=" + $.cookie("region") + "&ciudad=" + $.cookie("ciudad")
      });
    }
  }
});

但它只是传递给#pronostico page-id,在哈希中没有参数。因此,我得到的页面没有它应该显示的信息。

提前致谢。

解决方法

是的,认情况下不支持散列的参数。我一直在使用以下插件来给我,而且它一直工作得很好,

jqm.page.params

更新 – 如何使用:

包含jqm.page.params.js后,我添加了以下代码

$(document).bind("pagebeforechange",function( event,data ) {
    $.mobile.pageData = (data && data.options && data.options.pageData)
        ? data.options.pageData
        : null;
});

所以例如,一个页面调用像:index.html#search?id = mysearchkeyword
现在可以在任何页面活动中访问这些信息我觉得:

$(document).on("pagebeforeshow","#firstpage",function(e,data){ 
    if ($.mobile.pageData && $.mobile.pageData.id){
      console.log("Parameter id=" + $.mobile.pageData.id);
    }
});

将打印“mysearchkeyword”到您的日志控制台。

希望这可以帮助!

PS:请注意,我不是附属于该插件或它的作者

编者注:作者有第二个代码块。在Jquery 1.9中,live被删除,所以我用.on语法更新了上面的示例。这是原来的:

$("#firstpage").live("pagebeforeshow",data){
    if ($.mobile.pageData && $.mobile.pageData.id){
        console.log("Parameter id=" + $.mobile.pageData.id);
    }
});

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...