JqueryMobile(JQM),Json并将数据传递到新页面 [英] JqueryMobile (JQM), Json and passing data to a new page
问题描述
希望大家一切都好,并动摇您的代码编辑器.
Hope you all are fine and rocking your code editors.
我的问题是如何将url rel传递给新页面以供将来使用?
My question is how to pass url rel to a new page to use it in future?
让我给您一个示例,该示例的效果将优于1000个单词.
Let me give you an example that will work better than 1000 words.
在页面"A"上,我有页面"B"的URL,要加载页面"B",html,我将该URL直接指向页面"B",作为<a href="page-B.html" rel="{{id}}">{{deptName}}</a>
(不要看{{}}
,这是小胡子.js标记).
On page "A" I have url to page "B", to load page "B" html I pointing that url straight to page "B" as <a href="page-B.html" rel="{{id}}">{{deptName}}</a>
(don't look at {{}}
it is Mustache.js tags).
因此,当我单击该链接时,它应能正常工作,将我指向"B"页面并加载新的html布局(与"A"页面不同).
So when I am clicking on that link it is working as it should, pointing me to page "B" and loading my new html layout (that is different from page "A").
现在,因为我要从JSON提取数据,所以我需要将rel="{{id}}"
传递给页面B,以告诉我要从JSON获得什么数据.
Now because I am fetching data from JSON I need to pass rel="{{id}}"
to a page-B to tell what data I want from the JSON.
任何好的想法或实用代码如何实现的?
Any good ideas or practical code how you do that?
谢谢!
P.S.
在$ .delegate的"B"页面上,我需要该ID来执行以下操作:
On page "B" in $.delegate I need that id to do something like that:
$.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);
}
});
推荐答案
我找到了一个简单的解决方案,它是 window.sessionStorage .
I found a simple solution and it is window.sessionStorage.
在我的特定情况下,我单击URL/链接,获取href(这是我的ID),并使用 window.sessionStorage 保存该内容,更改 window.location 转到新页面并执行JSON魔术.
In my particular case scenarion I am clicking on URL/link, getting href (this where my id is), saving that with window.sessionStorage, changing window.location to the new page and do my JSON magic.
代码:
在"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!');
}
});
瓦拉!希望能对某人有所帮助.
Vuala! Hope that will help someone.
这篇关于JqueryMobile(JQM),Json并将数据传递到新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!