我有一个按钮,可以打开一个包含另一个html文件的对话框。我希望能够做的是向对话框传递一个变量。这就是我所拥有的。
var html = "";
var PLAN_ID = '1234';
html += "<div>"
html += '<a href="formFinal.html?Planid=' + PLAN_ID +'" data-rel="dialog" data role="button">Final</a>'
html += "</div>"
PLAN_ID变量是我希望推送到formFinal.html的变量。我可以用以下代码填充对话框上的隐藏文本输入。
$(document).on('pageshow', '#FinalPostPage', function(e) {
var page = $(this);
var query = page.data("url").split("?")[1];
var planid = query.split("=")[1];
$("input[name=Title]",this).val(planid);
})
我可以使用它,但直到formFinal.html页面加载后,变量才会填充。我需要在页面开发时使用该变量。希望这是有道理的。
您可以使用localStorage
将数据从该页面传输到另一个页面。
创建按钮时,将PLAN_ID
变量作为data-*
属性添加到a
,并为其创建一个id/class:
var html = "";
var PLAN_ID = '1234';
html += "<div>"
html += '<a href="formFinal.html" id="dialog-send" data-planid="' + PLAN_ID + '" data-rel="dialog" data role="button">Final</a>'
html += "</div>"
为按钮点击编写click
事件:
$(document).on("click", '#dialog-send' ,function(e) {
//prevent default action. we dont want it to redirect. Just yet.
e.preventDefault();
//set item in localStorage.
localStorage["plan_id"] = $(this).data("planid");
//then use changePage to redirect
$.mobile.changePage(this.href, {transition: 'pop', role: 'dialog'});
});
在您的pageshow
活动中,
$(document).on('pageshow', '#FinalPostPage', function(e) {
var page = $(this);
//var query = page.data("url").split("?")[1];
//get plan id from localStorage
var planid = localStorage["plan_id"];
$("input[name=Title]",this).val(planid);
});
希望这能有所帮助。