4

我正在尝试使用来自 jquery调用的 jquery 将一些数据附加到特定类options的菜单中。但是当我尝试调用它时,我得到了它没有附加的错误。或者,更准确地说,它根本没有被应用,因为下面的倒数第二行代码给了我错误selectdata()ajax()console.log($(this).data('address'.vendorName));Uncaught TypeError: Cannot read property 'vendorName' of undefined

内部$.each()循环是最后一次尝试,因为我认为可能只是 using $('.vendor_address_id_' + id).data....并没有应用于该类的所有元素。但我真的不认为它应该是必要的。我究竟做错了什么?

$.each(returnedData, function (key, val) {
    var id = val.id;

    //attach address information to each select option for display in .vendor_full_address_table
    $('.vendor_address_id_' + id).each(function (k, v) {

        $(this).data('address', {
            'vendorName': val.vendor_name,
            'address1': val.address1,
            'address2': val.address2,
            'city': val.city,
            'state': val.state,
            'zip': val.zip
        });
        //gives error: Uncaught TypeError: Cannot read property 'vendorName' of undefined 
        console.log($(this).data('address').vendorName);
    });
});



//console.log(returnedData);
Object
    address_0: Object
        address1: "street address1"
        address2: ""
        city: "Kalamazoo"
        id: "15"
        state: "MI"
        vendor_name: "companyA"
        zip: "123456"
    address_1: Object
        etc...
        etc...
        etc...
    address_2: Object
        etc...
        etc...
        etc...
4

1 回答 1

1

代替

val.vendor_name

采用

returnedData.vendor_name

这里valreturnedData[key]是迭代中特定值的值。

您正在尝试从原始类型访问属性,即此处的val

于 2012-11-14T17:47:44.950 回答