如何解决错误“Uncaught TypeError:无法调用方法'changePage'的undefined”

时间:2014-05-28 06:13:25

标签: javascript jquery html eclipse cordova

我只是在phonegap和javascript中的初学者,所以在制作一个简单的应用程序时,我在index.html头部编写了以下代码,我运行代码时,我的main.js文件没有被包含。我希望有人能帮我解决这个问题。

    <!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.min.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="assests/www/main.js"></script>
        </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>
                $("#loginPage").live("pageinit", function(e) {
                   checkPreAuth();
                });
            </script>
        </body>
    </html>

这里也是main.js文件代码。我只想要一个特定的登录ID才有效。

   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 = "burden123";
   var str2 = "game1234";

  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");
   }

2 个答案:

答案 0 :(得分:1)

<script type="text/javascript" charset="utf-8" src="assests/www/main.js"></script>

您的代码中的这一行应更改为

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

因为主要的js位于index.html页面的同一文件夹中。

答案 1 :(得分:0)

<script type="text/javascript" charset="utf-8" src="assests/www/main.js"></script>

在上面检查你在src中的路径也尝试把它放在上面并在main.js的顶部发出警告

检查上面的所有脚本是否包括在内。

由于