2014-05-25 49 views
0

我正在將表單中的數據存儲到本地存儲中,但是當我嘗試調用數據進行編輯時,它只會將數據加載到最後Id,並且不加載特定項目的數據。如何從本地存儲中獲取特定的Id數據以便在使用jquery的表單中進行編輯

這裏是我的編輯功能 `//用戶輸入編輯的形式 $( '#edit_employee_form')。在( '點擊',函數(){

var editEmployee = JSON.stringify({ 
     id: employees.length, 
     employeeno: $('#employeeno').val(), 
     employeename:$('#employeename').val(), 
     stateoforigine:$('#stateoforigine').val(), 
     employeephone: $('#employeephone').val(), 
     dateofbirth:$('#dateofbirth').val(), 
     }); 

//Alter the slected data 
localStorage.setItem("employees", JSON.stringify(employees));  
      return true; 
    }) 

for (var i in employees){ 

VAR EMPDATA =代碼JSON.parse(localStorage.getItem(僱員[I]));} '

這是我的代碼用於編輯數據

//register Edit button 
$('.edit_button').live('click', function(e){ 
    //alert('I was Cliked!'); 
     e.stopPropagation(); 
    var id = $(this).attr('href').substring(7); 

    var data = JSON.parse(localStorage.getItem('employees')); 
    $.each(data, function(a, b){ 
     $('#employeeno').val(b.employeeno); 
     $('#employeename').val(b.employeename); 
     $("#stateoforigine").val(b.stateoforigine); 
     $('#employeephone').val(b.employeephone); 
     $('#dateofbirth').val(b.dateofbirth); 
     $('#id').val(b.id); 
     $("#id").attr("readonly","readonly"); 
     $('#employeeno').focus(); 

     $.mobile.changePage('#add_employee_page'); 
       return true;` 

這裏是我的本地存儲

[{"id":1,"employeeno":"DEF/234/20014","employeename":"Bill Gates","stateoforigine":"Osun","employeephone":"080765432","dateofbirth":"12/11/1965"},{"id":2,"employeeno":"DEF/234/20014","employeename":"Bill Gates","stateoforigine":"Osun","employeephone":"080765432","dateofbirth":"12/11/1966"},{"id":3,"employeeno":"DEF/234/20014","employeename":"Bill Gates","stateoforigine":"Osun","employeephone":"080765432","dateofbirth":"12/11/1966"},{"id":4,"employeeno":"DAST/003/2003","employeename":"Gold Base","stateoforigine":"","employeephone":"","dateofbirth":"12/03/1986"}, 

本地存儲的關鍵是員工。

我該如何動態調用id的?謝謝

回答

0

在您的代碼中,您將覆蓋您的控制任務三次,最後一個條目勝出。你需要做的是指定你應該使用哪個ID來標識合適的員工並將其值映射到你的表單控件:

$.each(data, function(a, b){ 
    if(b.id === YOUR_ID){ 
    $('#employeeno').val(b.employeeno); 
    ... 
    return false; //Break out of the loop once you have the data you need 
    } 
+0

我應該評論這些行// var id = $(this)。 。ATTR( 'href' 屬性)子(7);和//var數據= JSON.parse(localStorage.getItem('employees'));?我在控制檯數據中得到這個錯誤未定義。 – user3650900

相關問題