将参数传递给jQuery Mobile中的页面ID [英] Passing parameters to a page-id in jQuery Mobile
问题描述
我正在尝试将一些参数传递给jQuery Mobile中的页面ID.
I'm trying to pass some parameters to a page-id made in jQuery Mobile.
该网站由带有链接的列表视图组成,每个视图都在其中编码了哈希,如下所示:
The site is composed of list-views with links, each of them has the hash coded in it, like this:
<li><a href="#pronostico?region=12&ciudad=0">Puerto Natales</a></li>
我绑定了pagebeforechange
来捕获URL中的哈希,执行参数检测并根据传递的参数数量采取措施.
I have binded pagebeforechange
to catch hashes in the URL, do parameter detection and take action depending on the amount of parameters passed.
现在,我正在尝试使用cookie:
Now, with cookies, I've been trying this:
$(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
页面ID,哈希中没有任何参数.结果,我得到的页面没有应该显示的信息.
But it just passes me to the #pronostico
page-id, with no parameters in the hash. As a result, I get a page without the information it is supposed to show.
谢谢.
推荐答案
是的,默认情况下不支持哈希中的参数.我一直在使用下面的插件给我,到目前为止,它一直工作得很好;-)
Yes, parameters on the hash are not supported by default. I've been using the following plugin to give me that, and it's been working pretty good so far ;-)
更新-使用方法:
在添加了jqm.page.params.js之后,我添加了以下代码:
I've added the following code after including 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 现在,我可以在任何页面事件中访问此信息:
So for example, a page getting called like: index.html#search?id=mysearchkeyword Can now access this information in ANY page event I feel like:
$(document).on("pagebeforeshow", "#firstpage", function(e, data){
if ($.mobile.pageData && $.mobile.pageData.id){
console.log("Parameter id=" + $.mobile.pageData.id);
}
});
将"mysearchkeyword"打印到您的日志记录控制台.
Would print "mysearchkeyword" to your logging console.
希望这会有所帮助!
PS:请注意,我与该插件无关,也不是它的作者
PS: Note that I am no way affiliated with the plugin or it's author
编辑者注意::作者将此作为第二个代码块.在Jquery 1.9中,live被删除,因此我改用.on语法更新了上面的示例.这是原始图片:
Editors Note: The author had this as second code block. In Jquery 1.9, live is removed so i updated his sample above with a .on syntax instead. Here is the original:
$("#firstpage").live("pagebeforeshow", function(e, data){
if ($.mobile.pageData && $.mobile.pageData.id){
console.log("Parameter id=" + $.mobile.pageData.id);
}
});
这篇关于将参数传递给jQuery Mobile中的页面ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!