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>

我尝试在 Eclipse 中使用 android sdk,并在模拟器上收到警报,并在添加新联系人后将其播种在联系人上。在您尝试此代码之前,请注意脚本和样式链接。(重写它,或下载最新的)

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 回答
-2

只需删除

 <body onload = "deviceIsReady()">

并做一个简单的

<body>

你还需要把你的

<script>

在头部或身体上,但不在他们之间!

您需要在设备上准备好事件,而不是在 bodylaod 上。所以试试这个:

    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <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>
        <script>
        document.addEventListener("deviceready",deviceIsReady, false);

        function deviceIsReady(){
           alert("READY!");
        }
        </script> 
   </head>
   <body>
   <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>

它应该有效(对我来说,它有效!)。如果没有,请检查清单,也许你做错了什么

于 2013-07-25T10:52:43.207 回答