我正在尝试根据是否选中表中的复选框将对象从一个本地存储移动到另一个本地存储。
// version 1.0
// Developer
// Date: Tuesday December 29th 2015
// Registeration for Cars and stuff as well as table to retreive the Car information
var Car = [];
function carReg(storagekey) {
alert('submitted');
"use strict";
// checks localstorage to see if there are already values and then keeps those values rather than deleteing them
//Car = JSON.parse(localStorage.getitem(storagekey));
// if (Car == null) {
// Car = [];
// }
// push all of the data into localStorage under the specific variable id's
Car.push({
Brand: document.getElementById("mySelect").value,
Model: document.getElementById("selectModel").value,
Age: document.getElementById("carAge").value,
KM: document.getElementById("km").value,
Name: document.getElementById("name").value,
ContactInfo: document.getElementById("cInfo").value,
})
// stores everthing into local storage under the specified key
localStorage.setItem(storagekey, JSON.stringify(Car))
}
/////////////////////////////
/// Table Code starts here//
///////////////////////////
function maketable() {
// get data from localStorage
var carReg = JSON.parse(localStorage.getItem("register"))
// Variables for the table values and classifications
var table, row, cell1, cell2, cell3, cell4, cell5, cell6;
table = document.getElementById('ownedCar')
// Row definers
for (var index = 0; index < carReg.length; index++) {
var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.id = index;
row = table.insertRow(index+1)
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
cell3 = row.insertCell(2);
cell4 = row.insertCell(3);
cell5 = row.insertCell(4);
cell6 = row.insertCell(5);
cell7 = row.insertCell(6);
// row classifications
cell1.innerHTML = carReg[index].Brand
cell2.innerHTML = carReg[index].Model
cell3.innerHTML = carReg[index].Age
cell4.innerHTML = carReg[index].KM
cell5.innerHTML = carReg[index].Name
cell6.innerHTML = carReg[index].ContactInfo
cell7.appendChild(checkbox);
}
}
function selling() {
//query from localStorage
var carReg = JSON.parse(localStorage.getItem("register"))
for (var index = 0; index < carReg; index++) {
if (!(document.getElementById(index).checked)) {
index = localStorage.setItem("sell", JSON.stringify(carReg))
}
}
}
基本上应该执行此操作的部分是这个。
function selling() {
//query from localStorage
var carReg = JSON.parse(localStorage.getItem("register"))
for (var index = 0; index < carReg; index++) {
if (!(document.getElementById(index).checked)) {
index = localStorage.setItem("sell", JSON.stringify(carReg))
}
}
}
但它没有做任何事情。我缺少什么吗?还是只是我的if语句部分错了?
最佳答案
我发现答案是我在 for 语句中缺少“.length”。谢谢瓦普西!
关于javascript - 有没有一种方法可以将对象从一个本地存储传输到另一个本地存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34780483/