JQuery Mobile pageShow和pageCreate事件未触发

时间:2013-03-28 12:04:48

标签: jquery jquery-mobile

我有一个JQuery Mobile应用程序。在应用程序的第一页上,我试图在加载时设置一些字段值,如果它们之前已经设置过的话。为了做到这一点,我目前有以下内容:

Index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">

    <link rel="stylesheet" href="/resources/css/themes/mobile/core.css" />
    <link rel="stylesheet" href="/resources/css/themes/mobile/app.css" />

    <script type="text/javascript" src="/resources/scripts/jquery-1.9.1.min.js"></script>  
    <script type="text/javascript" src="/resources/scripts/app.ui-1.0.js"></script>     
    <script type="text/javascript" src="/resources/scripts/jquery.mobile-1.3.0.min.js"></script>
  </head>
  <body>
    <div id="loginPage" data-role="page">
      <div data-role="header" data-position="fixed"><h1>AppName</h1></div>
      <div data-role="content">
        <label for="usernameTextBox">Username</label>
        <input id="usernameTextBox" type="text" autocomplete="false" /><br />

        <label for="passwordTextBox">Password</label>
        <input id="passwordTextBox" type="password" /><br />

        <div class="ui-grid-a"><div class="ui-block-a">
          <a href="#" id="loginButton" data-role="button" class="dd-btn-a" onclick="return loginButton_Click();">Login</a></div>
        </div>
      </div>
    </div>
  </body>
</html>

app.ui.js

$("#loginPage").on("pagecreate", function () {
    alert("here 1");
    $.mobile.hidePageLoadingMsg();
});

$("#loginPage").on("pageshow", function () {
    alert("here 2");
    $.mobile.hidePageLoadingMsg();

    var us = false;
    var ps = false;

    var u = window.localStorage.getItem("username");
    if ((u != null) && (u != undefined) && (u.length > 0)) {
        if (u != "undefined") {
            $("#usernameTextBox").val(u);
            us = true;
        }
    }
    alert(u);

    var p = window.localStorage.getItem("password");
    if ((p != null) && (p != undefined) && (p.length > 0)) {
        if (p != "undefined") {
            $("#passwordTextBox").val(p);
            ps = true;
        }
    }

    if ((us == true) && (ps == true)) {
        loginButton_Click();
    }
});

奇怪的是,我的“警报”框都没有被解雇。另外,我在控制台窗口中看不到任何错误。我做错了什么?

2 个答案:

答案 0 :(得分:4)

app.ui-1.0.js结束正文标记之前导入</body>。然后它会工作。原因是你的js文件将在加载DOM和jQuery文件之前执行。所以它不会开火。

始终确保按以下顺序导入

  1. jQuery框架
  2. jQuery Mobile framework
  3. jquery plugins
  4. 您的自定义js文件
  5. 正如Gajotres所提到的那样,将你的代码包装在下面的代码片段中作为一种良好的做法。

    $(document).on("pagecreate", "#loginPage", function () { 
    // your code here
    });
    

    如下所示

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
    
        <link rel="stylesheet" href="/resources/css/themes/mobile/core.css" />
        <link rel="stylesheet" href="/resources/css/themes/mobile/app.css" />
    
        <script type="text/javascript" src="/resources/scripts/jquery-1.9.1.min.js"></script>   
        <script type="text/javascript" src="/resources/scripts/jquery.mobile-1.3.0.min.js"></script>
      </head>
      <body>
        <div id="loginPage" data-role="page">
          <div data-role="header" data-position="fixed"><h1>AppName</h1></div>
          <div data-role="content">
            <label for="usernameTextBox">Username</label>
            <input id="usernameTextBox" type="text" autocomplete="false" /><br />
    
            <label for="passwordTextBox">Password</label>
            <input id="passwordTextBox" type="password" /><br />
    
            <div class="ui-grid-a"><div class="ui-block-a">
              <a href="#" id="loginButton" data-role="button" class="dd-btn-a" onclick="return loginButton_Click();">Login</a></div>
            </div>
          </div>
        </div>
            <script type="text/javascript" src="/resources/scripts/app.ui-1.0.js"></script>   
      </body>
    </html>
    

答案 1 :(得分:2)

将您的代码更改为:

$(document).on("pagecreate", "#loginPage", function () {
    alert("here 1");
    $.mobile.hidePageLoadingMsg();
});

$(document).on("pageshow", "#loginPage", function () {
    alert("here 2");

因为在将HTML加载到DOM之前绑定了页面事件。并移动这一行:

<script type="text/javascript" src="/resources/scripts/app.ui-1.0.js"></script>

在jQuery移动脚本标记下面。