繁体   English   中英

在PhoneGap 2.9中使用jQuery / JavaScript的单击按钮事件不起作用

[英]Click button event using jQuery/JavaScript in PhoneGap 2.9 does nothing

我正在使用Phonegap 2.9和Eclipse Juno开发适用于android的移动应用程序。 我看到许多有关使用Javascript和JQuery进行按钮单击事件的帖子,但似乎没有任何效果。 我的意思是,当我单击按钮时,没有警报出现! 我也尝试了<a href=...<input ...<button type="submit" ... ,但它们都不适用于相同的.js


我在.html文件中的头部是这样的:

 <head>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">

    <title>Sign-Up</title>

    <link href='http://fonts.googleapis.com/css?family=Patrick+Hand|Permanent+Marker|Exo|Nunito|Limelight|Ubuntu|Montserrat|Audiowide|Architects+Daughter' rel='stylesheet' type='text/css'>

    <!-- import stylesheets -->
    <link rel="stylesheet" type="text/css" href="../css/sign-up.css">
    <link rel="stylesheet" type="text/css" href="../css/sign-up-eng-fonts.css">

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
    <link rel="stylesheet" href="../jQuery-Mobile-Bootstrap-Theme-master/themes/Bootstrap.css">

    <!-- import js --> 
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>

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

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

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

 </head>

我当前在.html文件中创建按钮的代码的一部分是:

                <div class="ui-body ui-body-b">
                    <fieldset class="ui-grid-a">
                        <div class="ui-block-a"><button type="submit" id="cancel" data-theme="d">Cancel</button></div>
                        <div class="ui-block-b">
                            <input type="button" value="Register" id="submitt" data-theme="a" /></div>
                            <!--<a href="#" data-inline="button" data-role="button" data-theme="a">Register</a>-->
                    </fieldset>
                </div>

connect.js包含以下代码:

document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
    //
    function onDeviceReady() {
        // checkConnection();

        document.addEventListener("offline", onOffline, false);
        document.addEventListener("online", onOnline, false);
    }

function onOffline() {
    navigator.notification.alert('Please enable your Wi-fi and re-open the application!', close_app, 'Wi-fi', 'Okay');
}

function close_app() {
    navigator.app.exitApp();
}

function onOnline() {
    }

我的sign-up.js包含以下内容:

document.addEventListener("deviceready", onDeviceReady, false);

 function onDeviceReady() {

        document.getElementById('submitt').addEventListener('click', checkk, false);

            function checkk(){
                 navigator.notification.alert("I am here.", null, 'Here', 'Okay');
            }

    }

问题:当我运行应用程序并按按钮时,页面保持原样。 它甚至不刷新或显示我编写的警报。


当我在sign-up.html文件中使用onclick="register()尝试其他解决方案并且在我的sign-up.js文件中使用函数register()时,Eclipse的LogCat会说: file:///android_asset/www/htmls/home.html: Line 1 : Uncaught ReferenceError: register is not definedhome.html是应用程序的第一页,但寄存器按钮在。 sign-up.html -只有在这里我称之为头sign-up.js (包含register() )-

终于设法解决了我的问题。
它与按钮无关。 问题出在JavaScript上。


  • 首先,我更改了在home.htmlsign-up.html调用JavaScript的顺序。
    (1.来自Web的JQuery,2。cordova.js,3。connect.js,4。所有其他javaScript)
    home.html

     <title>Guide-Me For-All</title> <link href='http://fonts.googleapis.com/css?family=Patrick+Hand|Permanent+Marker|Exo|Nunito|Limelight|Ubuntu|Montserrat|Audiowide|Architects+Daughter' rel='stylesheet' type='text/css'> <!-- import stylesheets --> <link rel="stylesheet" type="text/css" href="../css/home.css"> <link rel="stylesheet" type="text/css" href="../css/home-eng-fonts.css"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css" /> <link rel="stylesheet" href="../jQuery-Mobile-Bootstrap-Theme-master/themes/Bootstrap.css"> <!-- import js --> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script type="text/javascript" charset="utf-8" src="../cordova.js"></script> <script type="text/javascript" charset="utf-8" src="../jss/connect.js"></script> <script type="text/javascript" charset="utf-8" src="../jss/change_page.js"></script> <script src="../plugins/page-swipe-iscroll/iscroll.js"></script> <script src="../plugins/page-swipe-iscroll/scroll.js"></script> 



  • home.html我将导航到另一页的方式更改为:

      <a href="#" data-role="button" data-theme="f" data-icon="heart" onclick="change_page();">Sign-Up Now!</a> 

这是必需的,因为当我在Google中阅读时,最好从JavaScript更改页面,而不是<a href="....."


  • 因此,我创建了change_page.js 以上是内容:

document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
//
function onDeviceReady() {
    // Empty
}

// alert dialog dismissed
function alertDismissed() {
    // do something
}

// Show a custom alert
//
function change_page() {
    document.location.href = '../htmls/sign-up.html';
}

  • 在我的sign-up页面中,我将按钮更改为:

      <a href="#" onclick="register();" data-role="button" data-theme="a">Register</a> 

  • 在我的sign-up.js文件中,我编写了以下代码:

    document.addEventListener("deviceready", onDeviceReady, false);

     // PhoneGap is ready // function onDeviceReady() { // Empty } // alert dialog dismissed function alertDismissed() { // do something } // Show a custom alert // function register() { navigator.notification.alert( 'hello!', // message alertDismissed, // callback 'Game Over', // title 'Done' // buttonName ); } 

因此,主要问题是导航到下一页的方式,因为JavaScript仅在第一个.html页中起作用。 希望它可以帮助遇到同样问题的任何人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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