问题
I spend my day in front of the following problem. I tried to get PhoneGap and jQuery-mobile running. The app starts but, if I submit the form he loads the same site again, ignoring everything in the form.js (see below the html-code). I have this html-Code:
<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>
It seems like he doesn't load the form.js as he isn't calling the function gotLoaded() nor console.logging anything. The form.js looks as like that:
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"
});
});
He does however load all the other scripts out of the js-folder, but isn't executing anything from the form.js. At least not on my Android-phone, in the Browser he is showing me the things I log. I hope you might be able to help me. Thanks a lot, stiller_leser
P.S. Just saw that he isn't logging ("transmitted form") either.
回答1:
you can try by adding the submit event handler in a pageshow handler(http://jquerymobile.com/demos/1.0rc3/docs/api/events.html) in the head, but you also need to give your page an 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>
回答2:
works for me as shown below. Got no idea why though. Seems like adding my form script at the end makes sure that all jQuery-Stuff is loaded. Here's the final code.
<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>
来源:https://stackoverflow.com/questions/14987629/phonegap-jquery-mobile-script-does-not-load