PhoneGap - 请告知保存功能无效的原因

时间:2012-05-31 12:59:26

标签: javascript jquery-mobile cordova

我使用VS2010编写一个小的示例,但我不知道为什么我不能使用window.locaStorage来存储和检索简单的值

请检查并告诉我有什么问题

    <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>

    <title>Cordova WP7</title>

      <link rel="stylesheet" href="jquery.mobile-1.1.0.css" type="text/css"/>
      <script src="jquery-1.7.2.js" type="text/javascript"></script>

      <script src="jquery.mobile-1.1.0.js" type="text/javascript"></script>


      <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>

      <script type="text/javascript">


        document.addEventListener("deviceready",onDeviceReady,false);

        // once the device ready event fires, you can safely do your thing! -jm
        function onDeviceReady()
        {
            //document.getElementById("welcomeMsg").innerHTML += "Cordova is ready! version=" + window.device.cordova;
            console.log("onDeviceReady. You should see this message in Visual Studio's output window.");
            //navigator.notification.alert("Hello World");

        };

        $(function(){

          $("#savebtn").click(function(){
            window.localStorage.setItem("modelname", $("#modelname").val());

          });

        $("#androidpg").live("pageshow", function () {
            var moname = ""
            moname = window.localStorage.getItem("modelname");

          if (moname.length > 0) {
              $("#modelname").val(moname);
          }

        });

      });

      </script>

  </head>
  <body>
    <div data-role="page" id="home">

      <div data-role="header">
        <h1>Home</h1>

      </div>
      <div data-role="content">
        This Demo can run on:
        <ul>
          <li>iOS</li>
          <li>Android</li>
          <li>BlackBerry</li>
          <li>Windows Mobile</li>
        </ul>
        <a href="#androidpg" data-role="button">Goto Android Page</a>

      </div>

      <div data-role="footer" data-position="fixed">
        <h4>All Rights Reserved</h4>

      </div>   

    </div>

    <div data-role="page" id="androidpg">

      <div data-role="header">
        <h1>Android</h1>

      </div>
      <div data-role="content">
        <h1>Android Page</h1>

        <label for="modelname">Enter Model Name:</label>
        <input type="text" name="modelname" id="modelname" value="" />
        <a href="#savebtn" data-role="button">Save</a>

      </div>

      <div data-role="footer" data-position="fixed">
        <h4>All Rights Reserved</h4>

      </div>   

    </div>

  </body>
</html>

由于

2 个答案:

答案 0 :(得分:0)

您可能需要注册“点击”或“点击”处理程序才能让您的处理程序触发。

答案 1 :(得分:0)

id-selector #savebtn查找ID为savebtn的元素。您的链接<a href="#savebtn" data-role="button">Save</a>的ID不是savebtn(您发布的代码中没有元素)。在锚点中添加ID。 <a href="#savebtn" id="savebtn" data-role="button">Save</a>