[英]JQuery Mobile - Can't get the parameters from the URL
我有一個動態創建的列表視圖,每個列表項都是這樣的鏈接: index.html#ClubPage?id=X
(其中X是int)。
我想在頁面加載之前獲取ID參數,但似乎該參數消失了。
$("#ClubPage").on("pagebeforeshow", function (event) {
alert( $(this).data("url") );
alert( window.location.href );
alert( window.location.search );
});
使用此代碼,第一個警報返回“ ClubPage”,第二個警報返回localhost/#ClubPage
,第三個localhost/#ClubPage
返回空白字符串。
如何獲取ID參數及其值? 似乎沒有任何效果:-\\
jQuery mobile為單個頁面模板處理url。 您將必須以其他方式傳遞參數。
一個如何做的例子:
<a href="#clubPage" param="3">Some Link</a>
您可以通過多種方式執行此操作,但是我已將id值放在“參數”下的鏈接中。
在下一節中,我將在單擊參數時將其拉出並將其存儲在sessionStorage中。 然后重定向到正確的頁面。 然后抓取參數“ beforepageshow”。
$('body').on('click','a[href="#clubPage"]',function(e){
e.preventDefault();
var param = $(this).attr('param');
sessionStorage.setItem('clubPage_param', param);
location.href = "index.html#ClubPage";
});
$("#ClubPage").on("pagebeforeshow", function (event) {
var param = sessionStorage.getItem('clubPage_param');
//now you have your item here
});
我敢肯定有多種方法可以執行此操作,您可以將鏈接保留在href中並進行剪切,然后再重定向到新頁面。 或將值存儲在javascript變量中。
您還可以制作單個頁面,然后以這種方式正常傳遞參數。 希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.