2015-09-01 78 views
0

我有一個應用程序從輸入字段收集數據並將其附加到表中。請參閱下面的代碼。我究竟做錯了什麼?SessionStorage問題,存儲乘法數據

 function save(){ 

       var name=  $("#name").val(); 
       var email=  $("#email").val(); 
       var telephone= $("#tel").val(); 
       var street= $("#street").val(); 
       var city=  $("#city").val(); 
       var state=  $("#state").val(); 
       var zip=  $("#zip").val(); 


       var inputArray = [name, email, telephone, street, city, state, zip]; 

       var dataToStore = JSON.stringify(inputArray); 

       var uniqueID = ID(); 

       sessionStorage.setItem(uniqueID, dataToStore); 

       display(); 


    }; 


function display(){ 

    var myArraySerialized = sessionStorage.getItem(uniqueID); 
    var myArray = JSON.parse(myArraySerialized); 

    var restoredName = myArray[0]; 
    var restoredEmail = myArray[1]; 
    var restoredTel = myArray[2]; 
    var restoredStreet = myArray[3]; 
    var restoredCity = myArray[4]; 
    var restoredState = myArray[5]; 
    var restoredZip = myArray[6]; 


    //append filled information from the form to the table and 2 buttons - Update and Remove 
     $("#listContent table").append("<tr>"+          
              "<td>" + restoredName + "</td>"+ 
              "<td>" + restoredEmail + "</td>"+ 
              "<td>" + restoredTel + "</td>"+ 
              "<td>" + restoredStreet + "</td>"+ 
              "<td>" + restoredCity + "</td>"+ 
              "<td>" + restoredState + "</td>"+ 
              "<td>" + restoredZip + "</td>"+ 
              "<td>" + "<button>Update</button>" + "</td>" + 
              "<td>" + "<button>Remove</button>" + "</td>" + 
              "</tr>"); 
    }      

var ID = function() { 

    return '_' + Math.random().toString(36).substr(2, 9); 
}; 

雖然控制檯說,UNIQUEID沒有定義的,我不明白爲什麼。當我使用任何其他變量 - 名稱,電子郵件,街道等 - 它通過,uniqueID - 不。我究竟做錯了什麼?而不是感謝您的幫助

回答

0

var ID = function() 

做:

function ID() 

否則你將無法調用它,因爲變量的時候JS是不確定的讀取代碼,其中你正試圖調用它。

要了解兩個函數聲明之間的差異,請參閱此question

還要注意的是var uniqueID = ID();是函數save的範圍內聲明所以display將無法​​看到它,也許你應該把它作爲參數:

//... 
display(uniqueID); 
//... 

//... 
function display (uniqueID) { 
//... 

爲了避免混亂,這裏是什麼完整的代碼看起來像:

function save() { 
    var name = $("#name").val(); 
    var email = $("#email").val(); 
    var telephone = $("#tel").val(); 
    var street = $("#street").val(); 
    var city = $("#city").val(); 
    var state = $("#state").val(); 
    var zip = $("#zip").val(); 
    var inputArray = [name, email, telephone, street, city, state, zip]; 
    var dataToStore = JSON.stringify(inputArray); 
    var uniqueID = ID(); 
    sessionStorage.setItem(uniqueID, dataToStore); 
    display(uniqueID); 
}; 


function display(uniqueID) { 
    var myArraySerialized = sessionStorage.getItem(uniqueID); 
    var myArray = JSON.parse(myArraySerialized); 
    var restoredName = myArray[0]; 
    var restoredEmail = myArray[1]; 
    var restoredTel = myArray[2]; 
    var restoredStreet = myArray[3]; 
    var restoredCity = myArray[4]; 
    var restoredState = myArray[5]; 
    var restoredZip = myArray[6]; 
    //append filled information from the form to the table and 2 buttons - Update and Remove 
    $("#listContent table").append("<tr>" + 
     "<td>" + restoredName + "</td>" + 
     "<td>" + restoredEmail + "</td>" + 
     "<td>" + restoredTel + "</td>" + 
     "<td>" + restoredStreet + "</td>" + 
     "<td>" + restoredCity + "</td>" + 
     "<td>" + restoredState + "</td>" + 
     "<td>" + restoredZip + "</td>" + 
     "<td>" + "<button>Update</button>" + "</td>" + 
     "<td>" + "<button>Remove</button>" + "</td>" + 
     "</tr>" 
    ); 
} 

function ID() { 
    return '_' + Math.random().toString(36).substr(2, 9); 
}; 
+0

VAR UNIQUEID = ID(),是的,這個變量diclared另一個函數裏面,它是隻看到這個功能裏面,但我用這個鍵設置中的數組sessionStorage,因此你niqueID應該與SessionStorage相關聯,並且可以在其他函數中重用,但不是 –

+0

這正是我想解釋的答案,變量uniqueID不能從要從sessionStorage中檢索數據的函數中看到,您需要通過它以某種方式顯示功能。 – taxicala