2013-02-14 66 views
0

當我必須打開此頁面時,我想要顯示手機中的所有聯繫人,無需點擊任何按鈕。 這裏myFunction()顯示所有聯繫人。Phonegap中的顯示聯繫人

I have to call `myFunction()`, in this code. I dont know where to call this function. Help me 

      var ar1 = new Array; 
      var ar2 = new Array; 
      var name, number; 
      var counter = 1; 

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

      function onDeviceReady() { 
       var options = new ContactFindOptions(); 
       options.filter = ""; 
       options.multiple = true; 
       filter = [ "displayName", "phoneNumbers" ]; 
       navigator.contacts.find(filter, onSuccess, onError, options);    
      } 

      function onSuccess(contacts) { 
       for (var i = 0; i < contacts.length; i++) { 
        for (var j = 0; j < contacts[i].phoneNumbers.length; j++) { 
         name = contacts[i].displayName; 
         number = contacts[i].phoneNumbers[j].value; 
         ar1.push(name); 
         ar2.push(number); 

// here i called myFunction(), but it's displaying one contact in multiple times      
         } 
        // here i called myFunction(), but it's displaying one contact in multiple times 
        } 
// Here i called myFunction(), the function is not calling 
      } 

      function onError(contactError) { 
       alert('onError!'); 
      } 

    // where to call this function 
      function myFunction() { 

       $("#checkall").click(function() { 
        if ($(this).is(':checked')) { 
         $(":checkbox").attr("checked", true); 
        } else { 

         $(":checkbox").attr("checked", false); 
        } 
       }); 

       for (var i = 0; i < ar2.length; i++) { 

        var newTextBoxDiv = $(document.createElement('div')).attr("id", 
          'TextBoxDiv' + counter); 
        newTextBoxDiv.after().html(
          '<input type="checkbox" value="' 
            + ar1[i] + '"/>' 
            + ar1[i] + " " + " " + ar2[i] + '</br>'); 
        newTextBoxDiv.appendTo("#TextBoxesGroup"); 

       } 
      } 
      </script> 
     </head> 
     <body> 

      </br> 

     <div id="TextBoxesGroup"> 
       <div id="TextBoxDiv1"> 
        <input type="checkbox" id="checkall" value="check" />selectAll</br> <br /> 
        <br /> <br /> 
       </div> 
      </div> 

      </body> 

     </html> 
+0

你的問題沒有說明究竟是什麼ü想要做的事。 – 2013-02-14 06:15:51

回答

0

我不明白你想要什麼。

如果您想在應用程序啓動時生成電話號碼複選框列表, 只需在onSuccess()回調結束時調用myFunction()。

如果你想要另一次,你應該在下面定義一個你想要的事件處理程序。

$("#PhonenumberListButton").click(function() { myFunction(); }); 

並且您的代碼可能在循環期間發生索引異常。

讓我們在下面考慮。

  1. 每個聯繫人都有1名,但有1個或多個電話號碼
  2. 代碼推每一個名字爲AR1,也推動接觸的每個電話號碼爲AR2
  3. 所以ar2.length可以比AR1更大.length
  4. 您的生成顯示代碼使用ar2.length for循環。如果任何聯繫人有2個或更多的電話號碼,它應該會例外。 這是停止onSuccess()中的循環的原因。

固定碼

 function onSuccess(contacts) { 
      for (var i = 0; i < contacts.length; i++) { 
       name = contacts[i].displayName; 
       ar1.push(name); 

       ar2[i] = []; // array for multiple phone#. 
       for (var j = 0; j < contacts[i].phoneNumbers.length; j++) { 
        number = contacts[i].phoneNumbers[j].value; 
        ar2[i].push(number); 
       } 
      } 

      myFunction(); // display phone numbers 
     } 

     function myFunction() { 

      $("#checkall").click(function() { 
       if ($(this).is(':checked')) { 
        $(":checkbox").attr("checked", true); 
       } else { 

        $(":checkbox").attr("checked", false); 
       } 
      }); 

      for (var i = 0; i < ar2.length; i++) { 
       if (ar2[i].length) { // avoid none phone# exception 
        var newTextBoxDiv = $(document.createElement('div')).attr("id", 
          'TextBoxDiv' + counter); 
        newTextBoxDiv.after().html(
          '<input type="checkbox" value="' 
            + ar1[i] + '"/>' 
            + ar1[i] + " " + " " + ar2[i][0] + '</br>'); 
        newTextBoxDiv.appendTo("#TextBoxesGroup"); 
       } 
      } 
     } 
+0

我稱myFunction(),onSuccess()結束,它不起作用。我必須顯示所有的聯繫人,只需點擊任何按鈕 – User 2013-02-14 06:37:35

+0

10我更新了有關您的代碼的答案。請讓我知道你的結果:) – cwdoh 2013-02-14 06:40:40

+0

在哪裏所有myFunction() – User 2013-02-14 06:47:01

相關問題