2

私はphonegapとjavascriptを初めて使用し、単純なContact Adderアプリケーションを作成しようとしていますが、何らかの理由で連絡先を追加しようとしても何も起こりません. アラートも表示されません。ところで、Eclipse 内で Android エミュレーターを使用してアプリケーションをテストしています。誰かが私が間違っていることを教えてもらえますか? ここに私のindex.htmlがあります:

<!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Add Contacts</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
        </head>
    <script>

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

     function deviceIsReady()
     {
    document.getElementById("save").addEventListener("click",addContact, false);
    alert("READY!");
      }
   function addContact()
    {
    var fullName = document.getElementById("first").value+ " " + document.getElementById("last").value;
    var theContact = navigator.contacts.create({"displayName" : fullName});
    theContact.save();
        alert("ADDED!");
     }
</script>           

   <body onload = "deviceIsReady()">
   <h1>Hello World</h1>

   <form>
    <label for="first">First</label><br>
    <input type="text" id="first"/><br>
    <label for="last">Last</label><br>
    <input type="text" id="last"/><br>
    <input type="button" value="Save Contact" id ="save"/>
    </form>
   </body>
</html>
4

3 に答える 3

0

このコードは私のために働いています:

<!DOCTYPE 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"  href="jquery.mobile-1.3.2.css">
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <script type="text/javascript" src="cordova.js"></script>
        <script src="jquery-1.10.2.min.js" language="javascript" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">
          var $j = jQuery.noConflict();
        </script>
        <script src="jquery.mobile-1.3.2.min.js" language="javascript" type="text/javascript"></script>

        <title>Add Contacts</title>
        <script>
            function loaded(){
                document.addEventListener("deviceready", deviceIsReady, false);
            }

            function deviceIsReady()
            {
                document.getElementById("save").addEventListener("click", addContact, false);
                alert("READY!");
            }
            function addContact()
            {
                var fullName = document.getElementById("first").value+ " " + document.getElementById("last").value;
                var theContact = navigator.contacts.create({"displayName" : fullName});
                theContact.save();
                alert("ADDED!");
            }
        </script>  
    </head>         

   <body onload = "loaded()">
   <h1>Hello World</h1>

   <form>
    <label for="first">First</label><br>
    <input type="text" id="first"/><br>
    <label for="last">Last</label><br>
    <input type="text" id="last"/><br>
    <input type="button" value="Save Contact" id ="save"/>
    </form>
   </body>
</html>

Android SDKでEclipseを試してみましたが、エミュレーターでアラートを受け取り、新しい連絡先を追加した後、連絡先に種をまきました。このコードを試す前に、スクリプトとスタイルのリンクに注意してください。(書き換えるか、最新のものをダウンロードしてください)

http://imageshack.us/a/img841/7938/b5x4.jpg

http://imageshack.us/a/img28/6060/9xfr.jpg

http://imageshack.us/a/img835/6729/yl8e.jpg

于 2013-09-03T21:42:11.183 に答える
-1

あなたのscriptタグはheadbodyタグの間にあります。body タグに移動してみてください。

ウィンドウの読み込みイベントもおそらく機能しません。それを削除して、デバイスの準備完了イベントを添付するだけで、スクリプトは次のようになります。

<script>
document.addEventListener("deviceready",deviceIsReady, false);

function deviceIsReady() {
    document.getElementById("save").addEventListener("click",addContact, false);
    alert("READY!");
}

function addContact() {
    var fullName = document.getElementById("first").value+ " " + document.getElementById("last").value;
    var theContact = navigator.contacts.create({"displayName" : fullName});
    theContact.save();
    alert("ADDED!");
}
</script> 
于 2013-05-23T22:28:12.273 に答える