0

我有一个应用程序,它从输入字段中收集数据并将其附加到表中。请在下面查看我的代码。我究竟做错了什么?

      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——没有。我究竟做错了什么?感谢帮助

4

1 回答 1

0

代替:

var ID = function () 

做:

function ID()

否则您将无法调用它,因为在 js 读取您尝试调用它的代码时,该变量将未定义。

要了解两个函数声明之间的区别,请参阅此问题

另请注意,它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);
};
于 2015-09-01T02:42:19.027 回答