有没有办法将对象从一个本地存储转移到另一个本地存储?

时间:2016-01-14 02:01:08

标签: javascript local-storage

我正在尝试将对象从一个localStorage移动到另一个localStorage,具体取决于是否勾选了表格中的复选框。

    // 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语句部分是错误的?

1 个答案:

答案 0 :(得分:0)

我发现答案是什么,我错过了一个&#39; .length&#39;在我的陈述中。谢谢wapsee!