这是将表单数据加载到字符串然后再加载的最佳方法localStorage吗?
我自己想出了这个,而且我不擅长编程.它适用于我需要的东西,但我不确定它是否是一个防弹代码?
<script>
var sg = document.getElementById("selectedGateway");
var sd = document.getElementById("selectedDestination");
var dm = document.getElementById("departureMonth");
var dd = document.getElementById("departureDay");
var dy = document.getElementById("departureYear");
var rm = document.getElementById("returnMonth");
var rd = document.getElementById("returnDay");
var ry = document.getElementById("returnYear");
var ad = document.getElementById("adults");
var ch = document.getElementById("option2");
$("#searchRequestForm").submit(function() {
var string = 'From: ' + sg.value + ' \nTo: ' + sd.value + ' \nDeparture: ' + dm.value + '/' + dd.value + '/' + dy.value + ' \nReturn: ' + rm.value + '/' + rd.value + '/' + ry.value + ' \nNumber of adults: ' + ad.value + ' \nNumber of children: ' + ch.value;
localStorage.setItem("string", string);
});
</script>
Run Code Online (Sandbox Code Playgroud)
我将使用类似下面的内容,以便我可以处理对象及其属性而不是大字符串.请注意,除了jQuery选择器之外,这是纯JavaScript.
演示: http ://jsfiddle.net/grTWc/1/
var data = {
sg: $("#selectedGateway").val(),
sd: $("#selectedDestination").val()
// items here
};
localStorage.setItem("mykey", JSON.stringify(data));
Run Code Online (Sandbox Code Playgroud)
要检索数据:
var data = JSON.parse(localStorage["mykey"]);
alert(data.sg);
Run Code Online (Sandbox Code Playgroud)
也可以看看:
| 归档时间: |
|
| 查看次数: |
88 次 |
| 最近记录: |