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");
    }
}
4

1 回答 1

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/

于 2013-08-19T13:09:07.950 回答