简体   繁体   English

JQuery Mobile-无法从URL获取参数

[英]JQuery Mobile - Can't get the parameters from the URL

I've a list-view that I created dynamically and each list item is a link like this: index.html#ClubPage?id=X (where X is int). 我有一个动态创建的列表视图,每个列表项都是这样的链接: index.html#ClubPage?id=X (其中X是int)。

I want to get the ID parameter before the page loads but it seems like the parameter vanish away. 我想在页面加载之前获取ID参数,但似乎该参数消失了。

$("#ClubPage").on("pagebeforeshow", function (event) {
    alert( $(this).data("url") );
    alert( window.location.href );
    alert( window.location.search );
});

With this code, the 1st alert returns "ClubPage", the 2nd returns localhost/#ClubPage and the 3rd returns blank string. 使用此代码,第一个警报返回“ ClubPage”,第二个警报返回localhost/#ClubPage ,第三个localhost/#ClubPage返回空白字符串。

How can I get the ID parameter and its value? 如何获取ID参数及其值? Nothing seems to work :-\\ 似乎没有任何效果:-\\

Jquery mobile manipulates the url for single page templates. jQuery mobile为单个页面模板处理url。 You will have to pass the parameter a different way. 您将必须以其他方式传递参数。

An Example of how to do it: 一个如何做的例子:

<a href="#clubPage" param="3">Some Link</a>

You can do this multiple ways, but I've put the id value in the link under "param". 您可以通过多种方式执行此操作,但是我已将id值放在“参数”下的链接中。

In the next section I am pulling the param out when they click and storing it in sessionStorage. 在下一节中,我将在单击参数时将其拉出并将其存储在sessionStorage中。 Then redirecting to the correct page. 然后重定向到正确的页面。 Then grabbing the param "beforepageshow". 然后抓取参数“ 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
});

I'm sure there are multiple ways to do this, you can keep the link in the href and cut it up before you redirect to the new page. 我敢肯定有多种方法可以执行此操作,您可以将链接保留在href中并进行剪切,然后再重定向到新页面。 Or store the value in a javascript variable. 或将值存储在javascript变量中。

You can also make single pages and pass the param normally that way. 您还可以制作单个页面,然后以这种方式正常传递参数。 Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM