我有两个以这种方式在父子关系中工作的HTML页面:
第一个具有一个按钮,该按钮执行以下两项操作:首先,它通过AJAX调用从数据库请求数据。其次,它将用户引导到具有所请求数据的下一页,JavaScript将对其进行处理以填充第二页。
我已经可以通过ajax调用获取数据并将其放入JSON数组中:
$.ajax({
type: "POST",
url: get_data_from_database_url,
async:false,
data: params,
success: function(json)
{
json_send_my_data(json);
}
});
function json_send_my_data(json)
{
//pass the json object to the other page and load it
}
我假设在第二页上,一个“文档就绪”的JavaScript函数可以轻松地处理所有数据的传递JSON对象的捕获。对我而言,最好的测试方法是在文档准备功能中使用
alert("My data: " + json.my_data.first_name);
来查看JSON对象是否已正确传递。我只是不知道执行此操作的可靠方法。我已经阅读了论坛,并且知道使用
window.location.url
加载第二页的基本知识,但是传递数据完全是另一回事了。 最佳答案
警告:这仅适用于单页面模板,其中每个伪页面都有其自己的HTML文档。
您可以通过手动使用$.mobile.changePage()
函数在页面之间传递数据,而不是让jQuery Mobile为您的链接调用它:
$(document).delegate('.ui-page', 'pageinit', function () {
$(this).find('a').bind('click', function () {
$.mobile.changePage(this.href, {
reloadPage : true,
type : 'post',
data : { myKey : 'myVal' }
});
return false;
});
});
这是此文档:http://jquerymobile.com/demos/1.1.1/docs/api/methods.html
您也可以简单地将数据存储在下一页的变量中。这是可能的,因为jQuery Mobile页面存在于同一DOM中,因为它们是通过AJAX引入DOM的。这是不久前我发布的有关此问题的答案:jQuery Moblie: passing parameters and dynamically load the content of a page