2013-08-19 40 views
1

我正在將數據存儲在本地存儲中。但是當我試圖從本地存儲中檢索數據時,數據不會進入輸入標籤。當頁面改變時,它應該到來。請幫助我。代碼中的錯誤是什麼?從本地存儲我沒有獲取輸入標記中的數據

在HTML5

<div data-role="page" id="page1"> 
    <div data-role="content"> 
     <form novalidate id="setting_form"> 
      <div data-role="fieldcontain"> 
       <label for="textusername1">Username</label> 
       <input name="textusername1" id="textusername1" value="" type="text"> 
      </div> 
      <div data-role="fieldcontain"> 
       <label for="textpassword1">Password</label> 
       <input name="textpassword1" id="textpassword1" value="" type="text"> 
      </div> 
      <div style="text-align:center;">      
       <div data-role="button" data-theme="b" data-inline="true" id="button1" onclick="callConnection();">Update</div> 
      </div> 
     </form> 
    </div> 
</div> 

<div data-rol2="page" id="show"> 
    <div data-role="content" id="content2"> 
     <form novalidate id="setting_form1"> 
      <label for="un">User Name</label> 
      <input type="text" name="un" class="field" id="un" value=""> 

      <label for="pw">Password</label> 
      <input type="text" name="pw" class="field" id="pw" value=""> 
    </div> 
</div> 

在jQuery中:

$(document).unbind('pageinit').bind('pageinit', function() {    
    $("#show").on("pageshow", function (event) { 
     $("#un").text(localStorage.getItem("user")); 
     $("#pw").text(localStorage.getItem("pass")); 
    }); 
}); 

function callConnection(){ 
    localStorage.setItem("user", $("#textusername1").val()); 
    localStorage.setItem("pass", $("#textpassword1").val()); 

    if (localStorage.getItem("user") == "" && localStorage.getItem("pass") == "") { 
     alert("enter the username and password"); 
    } 
    else { 
     $.mobile.changePage("#show"); 
    } 
} 

回答

2

如果你想改變,輸入值必須使用val,所以更改:

$("#un").text(localStorage.getItem("user")); 
$("#pw").text(localStorage.getItem("pass")); 

至:

$("#un").val(localStorage.getItem("user")); 
$("#pw").val(localStorage.getItem("pass")); 

文檔:http://api.jquery.com/val/#val-value

演示:http://jsfiddle.net/IrvinDominin/rg3n6/

+0

thanx的幫助,現在我爲得到答案 –

相關問題