changePage未定义

时间:2014-05-30 07:36:28

标签: javascript android jquery cordova

我正在尝试在phonegap上构建一个简单的应用程序,当我运行代码时,我在logcat中收到错误“未捕获的TypeError:无法调用未定义的方法'changePage'”。这是我的index.html和main.js.我在main.js的函数中得到了这个错误。

的index.html

<!DOCTYPE HTML>
<html>

<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Auth Demo</title>
<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.0rc2.css" type="text/css" charset="utf-8" />
<script type="text/javascript" src="js/jquery-1.7.1.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
<script src="jquery.mobile/jquery.mobile-1.0rc2.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>

<style>
 body
{
background-color:#d0e4fe;
 }

h1
{
color:Red;
text-align:center;
 }

</style>
</head>

<body onload="init()">


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

  <div data-role="header">
     <h1>Welcome to Phonegap</h1>
 </div>

<div data-role="content">    

    <form id="loginForm">
    <div data-role="fieldcontain" class="ui-hide-label">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" value="" placeholder="Username" />
    </div>

    <div data-role="fieldcontain" class="ui-hide-label">
        <label for="password">Password:</label>
        <input type="password" name="password" id="password" value=""   placeholder="Password" />
    </div>

    <input type="submit" value="Login" id="submitButton">
    </form>

</div>


   <script>
     src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"

 $("#loginPage").live("pageinit", function(e) {
    checkPreAuth();
    });
</script>



</body>
</html>

main.js在这里:

   function init() {
document.addEventListener("deviceready", deviceReady, true);
delete init;
   }

  function checkPreAuth() {
console.log("checkPreAuth");
    var form = $("#loginForm");
  if(window.localStorage["username"] != undefined && window.localStorage["password"] !=   undefined) {
{   
      $("#username", form).val(window.localStorage["username"]);
      $("#password", form).val(window.localStorage["password"]);
      navigator.notification.alert("You entered a username and password");
       handleLogin();
     }
      }
     }

     function handleLogin() {
    var form = $("#loginForm");    
     //disable the button so we can't resubmit while we wait
     $("#submitButton",form).attr("disabled","disabled");
      var u = $("#username", form).val();
      var p = $("#password", form).val();
     var str1 = "bidray";
    var str2 = "fivestars123";

  var n1 = str1.localeCompare(u);
  var n2 = str1.localeCompare(p);
   if(u != '' && p!= '') {
      $.post("http://www.coldfusionjedi.com/demos/2011/nov/10/service.cfc? method=login&returnformat=json", {username:u,password:p}, function(res) {
         if(n1==0 && n2==0) {
                 $.mobile.changePage("some.html");
         } else {
             navigator.notification.alert("Your login failed", function() {});
         }
      $("#submitButton").removeAttr("disabled");
    },"json");
 } else {
     navigator.notification.alert("You must enter a username and password", function() {}); 
     $("#submitButton").removeAttr("disabled");
   }
  return false;
 }

 function deviceReady() {
 console.log("deviceReady");
$("#loginPage").on("pageinit",function() {
    console.log("pageinit run");
    $("#loginForm").on("submit",handleLogin);
    checkPreAuth();
});
$.mobile.changePage("#loginPage");
   }

问题出在main.js的最后一行。 changePage未定义。 任何帮助,将不胜感激。 感谢。

0 个答案:

没有答案