[英]Phonegap ajax form not working
我的index.html中包含以下html / javascript
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<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/index.css" />
<title>Hello World</title>
<script type="text/javascript">
function PostData() {
// 1. Create XHR instance - Start
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
throw new Error("Ajax is not supported by this browser");
}
// 1. Create XHR instance - End
// 2. Define what to do when XHR feed you the response from the server - Start
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status == 200 && xhr.status < 300) {
document.getElementById('div1').innerHTML = xhr.responseText;
}
}
}
// 2. Define what to do when XHR feed you the response from the server - Start
var userid = document.getElementById("userid").value;
// 3. Specify your action, location and Send to the server - Start
xhr.open('POST', 'localhost:3000/ajax');
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("userid=" + userid);
// 3. Specify your action, location and Send to the server - End
};
</script>
</head>
<body>
<div class="app">
<h1>Ajax Testarea</h1>
<form>
<label for="userid">User ID :</label>
<input type="text" name ="userid" id="userid" />
<div id="div1"></div>
<input type="button" value ="Check" onclick="PostData()" />
</form>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
这在我的Mac上运行正常,但是当我尝试使其与phonegap配合使用时,我无法单击按钮来发送表格。 因此,它将不会启动PostData()函数。
我也不确定将Javascript直接放在html内是否是个好习惯,还是将它放在index.js内更好? 我读过有关将其放入其中的信息:
onDeviceReady: function() {
app.receivedEvent('deviceready');
// ---My Code---
},
如果有人可以指出我做错了事,我将非常高兴!
我在iOS版Phonegap中使用onClick和Jquery的Click函数时遇到了问题。 我不得不将jQuery .On()函数与touchstart一起使用
$('#buttonID').on({ 'touchstart' : function(){ /* Do stuff... */ } });
看起来您使用的不是jQuery,因此可以尝试使用onTouch="function"
或onTouchStart="function"
代替onClick。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.