localStorage是用来作为本地存储来使用的,是html5新加的一个特性。可以用来进行页面间值的传递。
当使用localStorage传递单个值时用法如下:
// 存储值
localStorage.setItem("test","test");
// 获取值
var test= localStorage.getItem("test");
当使用localStorage传递对象时,稍有不同:
// 存储时,要先将对象json化字符串
localStorage.setItem("test",JSON.stringify(result));
// 获取时,要将字符串json化对象
var test= JSON.parse(localStorage.getItem("test"));
页面取出值:
<input name="reportName" id="reportName" οnblur="checkRepeat()" class="form-control select2" data-placeholder="请选择"
data-parsley-multiple="labTeachReport"
data-parsley-errors-container="#reportNamecourseContainer" required>
</input>
function checkRepeat() {
//alert(2222);
//debugger;
// 获取值
var listLocalStorage = encode(localStorage.getItem("strLocalStorage"));
//console.log(listLocalStorage);
var reportName = $(" input[ name='reportName' ] ").val();
//var reportName = $("#reportName").val();
//console.log(reportName);
if (reportName != null && reportName != "") {
for(i=0; i < listLocalStorage.length; i++) {
var t = listLocalStorage[i];
//console.log(t);
if (t == reportName) {
alert("实验报告标题已存在,请从新输入!");
}
};
}
}
function encode(str){
var arr1 = str.split(',');
return arr1;
}
页面存入值:
onLoadSuccess: function (data) {
debugger;
console.log(data);
var list = data.rows;
var strLocalStorage = "";
for(i=0; i < list.length; i++) {
console.log(list[i].reportName);
strLocalStorage += list[i].reportName+",";
};
// 存储值
localStorage.setItem("strLocalStorage",strLocalStorage);
},