2016-01-14 98 views
3

我試圖根據是否勾選表格中的複選框將對象從一個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語句部分是錯誤的?

+0

好的,但carReg的數據類型是什麼,因爲如果它是一個字符串,你不能在你的循環中使用index

+0

index是一個整數,你確定你有複選框整數id-s?將一些console.log添加到代碼中,您會看到是否在for循環中輸入if。還打印carReg。你確定它是一個整數? – Gavriel

+0

我試過console.log,它甚至沒有進入if語句。也是一個字符串,所以你建議我做什麼,而不是索引 JKer

回答

0

我發現答案是我在我的陳述中缺少'.length'。感謝wapsee!

相關問題