检索存储在localStorage中的文本输入值

时间:2017-02-08 06:48:46

标签: javascript jquery local-storage

我正在尝试解析来自url的JSON数据(仅从JSON提供数据)并将值存储在localstorage中我现在尝试将存储在localstorage中的值从一个文件检索到另一个文件存储的值是来自数组对象。虽然检索我只获得其他文件中的最终对象。任何人都可以帮我如何检索所有对象?我附加了new.html下面的代码(这里我试图将数据存储在localstorage中)和new1.html(这里我正在检索数据)。谢谢

new.html:

<html>
<head>
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width ,height=device-height"/>

</head>

<body>
    <div id="header">
        <h1> Login </h1>
    </div>

    <div id="section">
    <!--script type="text/javascript" 
    charset="utf-8" src="cordova-1.7.0.js"></script-->
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>

    <script type="text/javascript" charset="utf-8">
        $(document).ready(function(){
            $("#submit").click(insertId);
        });

        function insertId() {
            // Getting the value of your text input
            var did = document.getElementById("deviceId").value;

            if (did == null || did == "") {

                alert("Kindly enter a valid Device ID");
                return false;

            }   else    {


                console.log("id: ", did);
            }

            window.alert("DID entered is : " + did);
            window.open("new1.html");

                            $.ajax({
                                    url : "https://api.thingspeak.com/channels/9/feeds.json?results=10",
                                    dataType:"json",
                                    cache: false,
                                    error:function (xhr, ajaxOptions, thrownError){
                                    debugger;
                                            alert(xhr.statusText);
                                            alert(thrownError);
                                        },
                                    success : function(json1) {
                                    console.log(json1);

                                    json1.feeds.forEach(function(feed, i) {

                                      console.log("\n The deails of " + i + "th Object are :  \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2);

                                            localStorage.setItem('Created_at', feed.created_at);
                                            var create = localStorage.getItem('Created_at');
                                            console.log(create);
                                            localStorage.setItem('Entry_id', feed.entry_id);
                                            var entry = localStorage.getItem('Entry_id');
                                            console.log(entry);
                                            localStorage.setItem('Field1', feed.field1);
                                            var fd1 = localStorage.getItem('Field1');
                                            console.log(fd1);
                                            localStorage.setItem('Field2', feed.field2);
                                            var fd2 = localStorage.getItem('Field2');
                                            console.log(fd2);
                                    });
                                    }
                                    });

        return false;
    }

    </script>

    <form id="insertId">
          <br><input type="text" placeholder="DeviceId" id="deviceId" /><br>
          <br>
          <input type="submit" id="submit" name="submit" value="Submit" />
    </form>
    </div>
</body>

new1.html:

<html>

<body onload="init();">
    <div id="header">
        <h1> USER DETAILS </h1>
    </div>

    <div id="section">

        <script>

            // Called on body's `onload` event
            function init() {
                // Retrieving the text input's value which was stored into localStorage

                var create = localStorage.getItem('Created_at');
                console.log(create);
                document.writeln("<br>Created_at  = "+create);
                var entry = localStorage.getItem('Entry_id');
                document.writeln("<br>Entry_id  = "+entry);
                var fd1 = localStorage.getItem('Field1');
                document.writeln("<br>Field1  = "+fd1);
                var fd2 = localStorage.getItem('Field2');
                document.writeln("<br>Field3  = "+fd2);



            }
        </script>

        <body onload="init();">

        </body>
    </div>
</body>

0 个答案:

没有答案