1

希望你们都很好,并且可以使用您的代码编辑器。

我的问题是如何将 url rel 传递给新页面以供将来使用?

让我举一个比 1000 字更好的例子。

在页面“A”上,我有指向页面“B”的 url,加载页面“B”html 我将该 url 直接指向页面“B” <a href="page-B.html" rel="{{id}}">{{deptName}}</a>(不要看{{}}它是 Mustache.js 标签)。

因此,当我单击该链接时,它会正常工作,将我指向页面“B”并加载我的新 html 布局(与页面“A”不同)。

现在因为我从 JSON 中获取数据,所以我需要传递rel="{{id}}"给 page-B 来告诉我想要从 JSON 中获取什么数据。

任何好的想法或实用的代码你是怎么做到的?

谢谢!

附言

在 $.delegate 的“B”页上,我需要该 id 来执行以下操作:

$.ajax({
        beforeSend: function() { $.mobile.showPageLoadingMsg() }, //Show spinner
        complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
        url: 'http://website.com/categoryJSON.ashx?&catId=THE_ID&callback=?',
        dataType: 'json',
        success: function(data) {
            var template = $('#pageCategoryTpl').html();
            var html = Mustache.to_html(template, data);
            $('[data-role=content]:first').html(html);
        }
    });
4

3 回答 3

2

尝试以下方式跨页面传输多个数据。

HTML

<a href="#detail?id=1234&another_id=5678" id="carrier-btn" data-role="button">link button with extra data</a>

JS

$(document).bind('pageshow', function(evt, data) {
    var id = queryParam("id"); //def of queryParam() in below fiddle
    var anotherId = queryParam("another_id");
    //console.log(id);
    //console.log("page show", evt, data);
    $(evt.target).find("#queryData").text(id);
    $(evt.target).find("#queryData1").text(anotherId);
});

完整源http://jsfiddle.net/dhavaln/MPmJH/

于 2012-06-06T12:52:20.897 回答
2

我找到了一个简单的解决方案,它是window.sessionStorage

在我的特定情况下,我单击 URL/链接,获取 href(这是我的 id 所在的位置),将其保存为window.sessionStorage,将window.location更改为新页面并执行我的 JSON 魔术。

编码:

在“A”页上:

$('a').live('click', function(){

        var currentID = $(this).attr('href');

        window.sessionStorage.setItem('parentId', currentID);

        window.location = 'page-B.html';

        return false;

        });

在“B”页上:

var parentId = sessionStorage.getItem('parentId');

$.ajax({
 url: 'http://www.url.com/file.json?id=' + parentId + '',
 dataType: 'json',
 success: function(data) {
    alert('Success!');
 }
});

瓦拉!希望这会帮助某人。

于 2012-06-07T08:24:46.667 回答
0

您可以使用全局变量来解决您的问题,而不是传递查询字符串(网址数据)。

前任:

// Your global variable 
var global_varible = '';

在页面 A 中,

全局变量=你的数据;

在页面 B 中,您可以访问 global_variable

于 2012-06-06T13:04:56.910 回答