PhoneGap - 请告知保存功能不起作用的原因

PhoneGap - please advise why the save function does not work

本文关键字:不起作用 功能 保存 PhoneGap      更新时间:2023-09-26

>我用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>

谢谢

id

选择器 #savebtn 查找 id 为 savebtn 的元素。您的链接<a href="#savebtn" data-role="button">Save</a>没有 id savebtn(您发布的代码中没有元素)。向定位点添加 ID。 <a href="#savebtn" id="savebtn" data-role="button">Save</a>

您可能需要注册"tap"或"vclick"处理程序才能触发处理程序。