PhoneGap - 未捕获的ReferenceError:函数未在文件中定义

时间:2013-03-04 15:13:50

标签: javascript function cordova

很抱歉,如果我重复已经解决的问题。我有基于phonegap的身份验证过程:

AVD:Android 4.2.2

<head>
    <script type="text/javascript" charset="utf-8" src="js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="js/jquery.mobile-1.2.0.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>
    <script type="text/javascript" charset="utf-8" src="main.js"></script>
</head>
<body>
<form id="loginForm">
    <div data-role="fieldcontain" class="ui-hide-label">
        <input type="text" name="username" id="username" value="" /></div>
    <div data-role="fieldcontain" class="ui-hide-label">
        <input type="password" name="password" id="password" value="" /></div>
    <div data-role="fieldcontain" class="ui-hide-label">
        <select name="coursetype" id="coursetype">
            <option value="1">Type 1</option>
            <option value="2">Type 2</option>
        </select></div>
    <!-- Below is 43th line -->
    <input type="submit" value="Login" id="submitButton" onclick="handleLogin();" />
</form>
</body>

使用 main.js 中的相应代码程序:

function handleLogin() {
    var form = $("#loginForm");
    var u = $("#username", form).val();
    var p = $("#password", form).val();
    var t = $("#coursetype", form).val();
    if(u!= '' && p!= '' && t!= '') {
        alert('Values are: ' +u+ ' & ' +p+ ' & ' +t);
    } else {
        alert('Field(s) were empty');
    }
}

点击登录按钮,会显示以下内容:

Uncaught ReferenceError: handleLogin is not defined at file:///android_asset/www/index.html:43

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试删除onclick,而不是在main.js

$('document').ready(function () {
    $('#submitButton').on('click',function (e) {
         e.preventDefault();
         var form = $("#loginForm");
         var u = $("#username", form).val();
         var p = $("#password", form).val();
         var t = $("#coursetype", form).val();
         if(u!= '' && p!= '' && t!= '') {
             alert('Values are: ' +u+ ' & ' +p+ ' & ' +t);
         } else {
             alert('Field(s) were empty');
         }
    })
})

我真的不相信onclick,很多原因,我认为可能是问题。