我想附加一些數據options
在select
特定類的菜單使用jquery data()
來自jquery ajax()
調用。但是當我嘗試調用它時,我收到了錯誤,它沒有被連接。或者,更準確地說,它是沒有得到所有在下面的代碼的antipenultimate線應用,爲console.log($(this).data('address'.vendorName));
給我的錯誤Uncaught TypeError: Cannot read property 'vendorName' of undefined
jquery數據()不附加值
內$.each()
循環,是因爲我想也許只是用 最後刺嘗試$('.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...
讓我們輕鬆:在jsFiddle上發佈此代碼並刪除您認爲不需要的代碼。 – jdigital