.value不返回任何值(javaScript)

.value Returning Nothing (javaScript)

本文关键字:javaScript 任何值 返回 value      更新时间:2023-09-26

我试图从联系人获取电话号码的值,但我要么得到[object, object]返回给我,要么什么都没有。当我尝试火柴[I]。phoneNumbers我得到[object, object],当我添加匹配[I].phoneNumbers[0]。值,我的警报完全停止。

function callme() {
    var options = new ContactFindOptions();
    options.filter = ""; //leaving this empty will find return all contacts
    options.multiple = true; //return multiple results
    console.log(options);
    var filter = ["displayName", "phoneNumbers"]; //an array of fields to compare against the options.filter 
    navigator.contacts.find(filter, successFunc, errFunc, options);
    function successFunc(matches) {
        for (var i = 0; i < matches.length; i++) {
            //this loops through all of the contacts
            var contact_name = matches[i].displayName;
            var contact_number = matches[i].phoneNumbers; //this returns [object, Object]
            // var contact_number = matches[i].phoneNumbers[0].value; returns nothing at all
            var contact_full = contact_name + " " + contact_number;
        }
        alert(contact_full);
    }
    function errFunc() {
        alert("oh no!");
    }
};

我不确定JSON对象的结构是什么,但你可以在firebug中调试,如果你不确定如何做到这一点,那么就试试下面的操作:

function successFunc(matches) {
        for (var i = 0; i < matches.length; i++) {
            //this loops through all of the contacts
        var contact_name = matches[i].displayName;
        alert(JSON.stringify(matches[i].phoneNumbers);
var contact_number = matches[i].phoneNumbers; //this returns [object, Object]
            // var contact_number = matches[i].phoneNumbers[0].value; returns nothing at all
            var contact_full = contact_name + " " + contact_number;
    }
    alert(contact_full);
}

一旦警报对话框出现,您将能够看到对象的属性列表。