使用javascript将数据从一个HTML页面传递到另一个HTML页面
我正在尝试从一个要求用户输入他/她的详细信息的HTML页面传递数据到另一个显示此数据的HTML页面。我正在使用JavaScript来做到这一点。一切正常,但数据没有出现在第二个HTML页面中。
注意:我正在使用ONSEN UI。
我尝试过:
JavaScript代码:
var allData = [];
document.addEventListener(init,函数(事件){
if(event.target.id ==details){
document.getElementById(name)。value = allData [1];
document.getElementById(mobile)。value = allData [2];
document.getElementById(date)。value = allData [3];
document.getElementById(section)。value = allData [4];
document.getElementById(issue)。value = allData [5];
}
},false);
document.getElementById(Sbtn)。onclick = function(){submitForm()};
函数submitForm()
{
//localStorage.clear();
allD ata = [];
var a = document.getElementById(name)。value;
var b = document.getElementById(mobile)。value ;
var c = document.getElementById(date)。value;
var d = document.getElementById(section)。value;
var e = document.getElementById(issue)。value;
allData.push(a);
allData.push(b);
allData.push(c);
allData.push(d);
allData.push(e);
localStorage.setItem('AllData',allData);
var options = {
animation:'slide',//使用什么动画
onTransitionEnd:function(){} //完成过渡动画时调用
};
myNav.pushPage(details.html,选项);
alert(请检查你的详情);
alert( allData);
}
------ --------------------
第二页HTML代码:
< ; ons-page id =details>
< ons-toolbar>
Hi,
I'm trying to pass data from one HTML page that asks the user to enter his/her details, to another HTML page that displays this data. I'm Using JavaScript to do that.Everything is working, but the data does not appear in the second HTML page.
Note: I'm Using ONSEN UI.
What I have tried:
JavaScript code:
var allData=[""];
document.addEventListener("init", function(event) {
if (event.target.id == "details") {
document.getElementById("name").value=allData[1];
document.getElementById("mobile").value=allData[2];
document.getElementById("date").value=allData[3];
document.getElementById("section").value=allData[4];
document.getElementById("issue").value=allData[5];
}
}, false);
document.getElementById("Sbtn").onclick= function(){submitForm()};
function submitForm()
{
//localStorage.clear();
allData=[""];
var a= document.getElementById("name").value;
var b= document.getElementById("mobile").value;
var c= document.getElementById("date").value;
var d= document.getElementById("section").value;
var e= document.getElementById("issue").value;
allData.push(a);
allData.push(b);
allData.push(c);
allData.push(d);
allData.push(e);
localStorage.setItem('AllData', allData);
var options = {
animation: 'slide', // What animation to use
onTransitionEnd: function() {} // Called when finishing transition animation
};
myNav.pushPage("details.html", options);
alert("Please check your details");
alert(allData);
}
--------------------------
Second HTML page Code:
<ons-page id="details">
<ons-toolbar>
< form>
< label>名称: < / label>
< label>手机号码: < / label>
< label>日期:< ; / label>
< label>部分:< / label>
< label>问题:< / label>
< / form>
你好,{{NAD || 'stranger'}}!
您的手机号码是{{mobile || 'null'}}
您的问题是{{issue || 'null'}}
<form>
<label>Name: </label>
<label>Mobile Number: </label>
<label>Date: </label>
<label>Section: </label>
<label>Issue: </label>
</form>
Hello, {{ NAD || 'stranger' }}!
Your mobile number is {{ mobile || 'null' }}
Your problem is {{ issue || 'null' }}
我想建议一种与服务器端无关的现代通用方法;它甚至可以在没有任何服务器端的情况下工作,当你只想恢复一些UI状态或任何数据之后,比如本地重新加载或任何其他需要恢复你以前状态的动作。
这个通用API是Web存储
:
网络存储 - 维基百科,免费的百科全书 [ ^ ],
Web Storage API - Web API | MDN [ ^ ],
Window.sessionStorage - Web APIs | MDN [ ^ ],
Window.localStorage - Web APIs | MDN [ ^ ]。
显然,出于您的目的,您必须更喜欢会话存储,而不是本地存储。本地存储会污染您的本地浏览器数据,但即使使用本地存储,您也可以轻松将其删除。
另外,您可能更合适地希望保留所有状态记录一键并保存一次恢复所有内容。这将是最好的主意。为此,您需要序列化和反序列化所有数据。可以使用JSON轻松完成: JSON - JavaScript | MDN [ ^ ]。
您可以在我的文章的Web存储部分找到整个技术的全面样本:
JavaScript Calculator, 7动态严格模式切换和网络存储 [ ^ ]
就是这样。-SA
I want to suggest one more modern and universal approach agnostic to the server side; it can even work without any server side at all, when you simply want to restore some UI state or any data at all after, say, local reload or any other action which need restoration of your previous state.
This universal API isWeb storage
:
Web storage — Wikipedia, the free encyclopedia[^],
Web Storage API — Web APIs | MDN[^],
Window.sessionStorage — Web APIs | MDN[^],
Window.localStorage — Web APIs | MDN[^].
Apparently, for your purpose you have to prefer session storage, not local storage. Local storage would contaminate your local browser data, but even with local storage you can easily wipe it out.
Also, you may reasonably prefer keeping all the state record by just one key and save restore everything at once. It would be the best idea. To do so, you would need to serialize and deserialize all data. It can be easily done with JSON: JSON — JavaScript | MDN[^].
You can find a comprehensive sample of the whole technique in my article, in the Web storage section:
JavaScript Calculator, 7 Dynamic Strict Mode Switching and Web Storage[^]
That's all.—SA