【问题标题】:from local storage I am not getting data in input tags从本地存储我没有在输入标签中获取数据
【发布时间】:2013-08-21 05:51:03
【问题描述】:

我将数据存储在本地存储中。但是当我尝试从本地存储中检索数据时,输入标签中没有数据。当页面发生变化时,它应该会到来。请帮帮我。代码有什么错误?

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

【问题讨论】:

    标签: jquery html jquery-mobile local-storage


    【解决方案1】:

    如果你想改变和输入值你必须使用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/

    【讨论】:

    • 感谢您的帮助,现在我得到了答案
    猜你喜欢
    • 2023-03-20
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多