简体   繁体   中英

PhoneGap - Uncaught ReferenceError: function is not defined at file

Sorry if I'm repeating the question that already has been solved. I've phonegap based authentication process:

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>

with the appropriate code procedure in 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');
    }
}

By clicking the login button it says the following:

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

Any help would be appreciated.

Try removing the onclick and instead have this in 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');
         }
    })
})

I just never really trust onclick, lots of reasons, I think its probably the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM