我有一个包含位于本地存储中的四个元素的数组。
function rm(t)
{
// var l = localStorage.getItem("subje");
// hide element from html page
document.getElementById(t).style.display = 'none';
var c = localStorage.getItem("class");
var item = c+""+t; // c is int and t is string
var subj = localStorage.getItem("std-sub");
var x = subj.indexOf(item);
/*
subj.splice(x,1);
localStorage.setItem("final", subj);
var k = localStorage.getItem("final");*/
document.getElementById('show').innerHTML = x;
}
当我执行函数时,x的值显示出来 29,17,7,0。但我给定的数组只有四个元素。 为什么?????
答案 0 :(得分:1)
null
始终返回字符串(或subj.indexOf(item);
)。因此item
将获得字符串subj
中子字符串$(document).ready(function () {
$("#btnimg").click(function () {
//check input file id number
var counter = $("input[id^='upload']").length;
//add input file every btnimg clicked
var html = "<input type='file' id='upload_" + counter + "' style='display:none;'/>";
$("#result").append(html);
//trigger to dialog open file
var upload = $('#upload_' + counter);
upload.trigger('click');
upload.on('change', function () {
console.log('change fire...');
var inputFiles = this.files;
var inputFile = inputFiles[0];
var reader = new FileReader();
reader.onload = function (evt) {
var imghtml = "<img id='img_upload_" + counter + "' src='" + evt.target.result + "' width='50px;' height='50px;'/>";
$('#previewimage').append(imghtml);
};
reader.onerror = function (event) {
alert("something: " + event.target.error.code);
};
reader.readAsDataURL(inputFile);
});
//if file not selected or user press button cancel on open dialog
//upload.remove();
});
});
的位置。您应该将数组编码并解码为JSON,并将其存储在本地存储中。