0

我用VS2010写了一个phonegap小例子但是我不知道为什么我不能使用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>

谢谢

4

2 回答 2

0

您可能需要注册一个“tap”或“vclick”处理程序才能让您的处理程序触发。

于 2012-05-31T14:52:33.470 回答
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>

于 2012-05-31T14:56:41.547 回答