簡體   English   中英

Phonegap AJAX表單不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM