Javascript 动态读取本地存储

Javascript 动态读取本地存储,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我在尝试动态读取HTML5本地存储时遇到问题 我对这个很陌生,可能不知道我在做什么,但我正在尝试阅读我在另一个页面上保存的内容 保存的内容的数量将取决于用户选择的内容,我希望能够读回给他们确认。类似于结账车 代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>L

我在尝试动态读取HTML5本地存储时遇到问题

我对这个很陌生,可能不知道我在做什么,但我正在尝试阅读我在另一个页面上保存的内容

保存的内容的数量将取决于用户选择的内容,我希望能够读回给他们确认。类似于结账车

代码如下:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Local Storage Load Test</title>
  <script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
      <link rel="stylesheet" type="text/css" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
      <script type='text/javascript' src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  <style type='text/css'>
  </style>



<script type='text/javascript'>//<![CDATA[ 

$(window).load(function(){
    $(document).on("pagebeforehide", "[data-role=page]", function () {
        // variables
        var elements = [],
            id = '',
            value = '',
            text = '';

        // push values of checkboxes and radio buttons into an array
        $("[type=checkbox], [type=radio]", this).each(function () {
            id = $(this)[0].id;
            value = $(this).prop("checked");
            text = $(this).next('label').text();
            elements.push({
                "id": id,
                    "value": value,
                    "text": text
            });
        });

        // convert array into a string
        // in order store it into localStorage
        localStorage["values"] = JSON.stringify(elements);
    });

    $(document).on("pagebeforeshow", "[data-role=page]", function () {
        // active page
        var active = $.mobile.activePage;

        // parse array of checkboxes and radio buttons
        var read_array = JSON.parse(localStorage["values"]);

        // check/uncheck checkboxes and radio buttons
        $.each(read_array, function (e, v) {
            var check_id = "#" + v.id,
                check_value = v.value,
                check_text = v.text;
            $(check_id, active).prop("checked", check_value).checkboxradio("refresh");
            if (check_value == true) {
                $(active).append("Checked text: " + check_text + "<br/>");
            }
        });
    });
});//]]>  

</script>

</head>
<body>
<div data-role="page" id="page2">
    <div data-role="header">
         <h1>Page 2</h1>
    </div>
    <div data-role="content">
        <script type='text/javascript'>
         for (i = 0; i < localStorage.length; i++)
        {
        document.write("<div data-role=\"fieldcontain\">");
                document.write("<form>");
                document.write("<fieldset data-role=\"controlgroup\" data-type=\"horizontal\">");
                    document.write("<legend>Levels:</legend>");
                    document.write("<input type=\"checkbox\" name=\"checkbox-h-2a\" id=\"checkbox-h-2a\" class=\"custom1\">");
                    document.write("<label for=\"checkbox-h-2a\">One</label>");
                    document.write("<input type=\"checkbox\" name=\"checkbox-h-2c\" checked=\"checked\" id=\"checkbox-h-2c\" class=\"custom1\">");
                    document.write("<label for=\"checkbox-h-2c\">Two</label>");
                    document.write("<input type=\"checkbox\" name=\"checkbox-h-2b\" id=\"checkbox-h-2b\" class=\"custom1\">");
                    document.write("<label for=\"checkbox-h-2b\">Three</label>");
                    document.write("<input type=\"checkbox\" name=\"checkbox-h-2d\" checked=\"checked\" id=\"checkbox-h-2d\" class=\"custom1\">");
                    document.write("<label for=\"checkbox-h-2d\">Four</label>");
                document.write("</fieldset>");
            document.write("</form>");
        document.write("</div>");
        }</script>
        <div data-role="fieldcontain">
            <form>
                <fieldset data-role="controlgroup" data-type="horizontal">
                    <legend>Mode:</legend>
                    <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2a" value="on" checked="checked" class="custom2">
                    <label for="radio-choice-h-2a">Steward</label>
                    <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2b" value="off" class="custom2">
                    <label for="radio-choice-h-2b">Guest</label>
                    <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2c" value="on" checked="checked" class="custom2">
                    <label for="radio-choice-h-2c">Option 1</label>
                    <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2d" value="off" class="custom2">
                    <label for="radio-choice-h-2d">Option 2</label>
                </fieldset>
            </form>
        </div> 
     <a href="test1.php" data-role="button">Previous Page</a>
    </div>
</div>    
</body>

</html>

本地存储负载测试
//  
第2页
对于(i=0;i
请让我知道我做错了什么,我应该做什么来修复它


谢谢

所以,我认为它不起作用;请您描述一下问题,您采取了哪些步骤来生成它,您测试了哪些浏览器,以及您是否尝试过调试…?对于我尝试动态创建的levels表单,我没有得到任何信息,然后它下面的mode表单将显示为没有任何格式。我正在使用Chrome。打开Chrome开发者控制台,在您正在读写
localStorage
的地方设置断点,并检查是否达到了预期的效果。我很想提供帮助,但似乎有太多的事情要解决。你能用jsfiddle.net把它分成更小的部分吗?我还建议通过设置断点和单步检查变量值进行调试。