PhoneGap& jQuery Mobile:脚本无法加载

时间:2013-02-20 18:48:59

标签: cordova jquery-mobile

我在以下问题面前度过了一天。我试图让PhoneGap和jQuery-mobile运行。应用程序启动但是,如果我提交表单,他会再次加载相同的站点,忽略form.js中的所有内容(请参阅下面的html代码)。 我有这个HTML代码:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.2.0.min.css" />
    <script type="text/javascript" src="js/jQuery/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="js/jQuery/jquery-migrate-1.0.0.js"></script>
    <script type="text/javascript" src="cordova-2.4.0.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/form.js"></script>
    <script type="text/javascript" src="js/jQuery.mobile/jquery.mobile-1.2.0.min.js"></script>
    <title>SecureBook</title>
</head>
<body onload="app.initialize();">
    <div data-role="page">
        <div data-role="header">
            <h1>Welcome</h1>
        </div>
        <div data-role="content">
            <form id="userdata">
                <input type="text" id="username" placeholder="Username"  autofocus>
                <input type="password" id="password" placeholder="Passoword">
                <input type="submit" value="Anmelden" data-icon="forward" data-theme="b">
            </form>
        </div>
        <script>
            gotLoaded();
            console.log("in HTML");
        </script>
    </div>
</body>

似乎他没有加载form.js,因为他没有调用函数gotLoaded(),也没有调用console.logging任何东西。 form.js看起来像那样:

function gotLoaded(){
console.log("loaded form");
};
$('#userdata').submit(function(event){
    console.log("transmitted form");
    event.preventDefault();
    var user = $('#username').val();
    var password = $('#password').val();
    $.mobile.changePage("sites/contacts.html",{
        transition: "none"
    });
});
然而,他确实从js文件夹中加载了所有其他脚本,但是没有从form.js执行任何操作。至少不在我的Android手机上,在浏览器中他向我显示我记录的内容。我希望你能帮助我。 非常感谢, stiller_leser

P.S。刚刚看到他没有登录(“传输形式”)。

2 个答案:

答案 0 :(得分:0)

您可以尝试在页眉显示处理程序(http://jquerymobile.com/demos/1.0rc3/docs/api/events.html)中添加提交事件处理程序,但您还需要为您的页面添加ID。

<script>
    $('#pageID').live("pageshow", function() {
        $('#userdata').submit(function(event){
            console.log("transmitted form");
            event.preventDefault();
            var user = $('#username').val();
            var password = $('#password').val();
            $.mobile.changePage("sites/contacts.html",{
                transition: "none"
            });
        });
    })
 </script>

答案 1 :(得分:0)

对我有用,如下所示。虽然不知道为什么。似乎在最后添加我的表单脚本确保加载所有jQuery-Stuff。这是最终的代码。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.2.0.min.css" />

    <script type="text/javascript" src="cordova-2.4.0.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/jQuery/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="js/jQuery/jquery-migrate-1.0.0.js"></script>  
    <script type="text/javascript" src="js/jQuery.mobile/jquery.mobile-1.2.0.min.js"></script>       
    <title>SecureBook</title>
</head>
<body onload="app.initialize();">
    <div data-role="page">
        <div data-role="header">
            <h1>Welcome</h1>
        </div>
        <div data-role="content">
            <form id="userdata" onsubmit="gotLoaded();">
                <input type="text" id="username" placeholder="Username"  autofocus>
                <input type="password" id="password" placeholder="Passoword">
                <input type="submit" value="Anmelden" data-icon="forward" data-theme="b">
            </form>
        </div>
        <script>
            //WORKS
            console.log("in HTML");
            //JQUERY works here
            console.log($('title').html());
        </script>
    </div>

<!-- Put customs scripts here as it seems, that jQuery loads to slow in the head and scripts already run -->
<script type="text/javascript" src="js/form.js"></script>
</body>