此代码块从 csv 获取数据,将其放入 JSON 并显示给用户。此外,我正在尝试创建一个对象属性并根据所选值分配其值,但我得到deliveryMethod - undefined
.
$(document).ready(function() {
$.getJSON('CSV.php', function(data) {
var prevCardCode = '';
var newDiv; var NewDiv2;
var DeliveryMethod;
$.each(data, function(index, element) {
var XMLObject = jQuery.extend({}, element);
if (element['CardCode'] != prevCardCode) {
newDiv = $('<div/>').addClass('row').appendTo('#showdata');
$('<div class="sum_field">' + 'Total: ' + element['payment_sum'] + '</div>').appendTo(newDiv);
$('<div class="options">Delivery Method: <select name="Delivery_Method" id="Delivery_Method"><option value="100" selected="selected">US Mail</option><option value="300">Foreign Mail</option></select></div>').appendTo(newDiv);
$('select#Delivery_Method').change(function(){
DeliveryMethod = $(this).val();
});
XMLObject.deliveryMethod = DeliveryMethod;
console.log(XMLObject);
}
newDiv2 = $('<div/>').addClass('sub_row').appendTo(newDiv);
prevCardCode = element['CardCode'];
$.each(element, function(key, value) {
switch (key) {
case 'InvKey':
break;
default:
$('<div class="field">' + value + '</div>').appendTo(newDiv2);
break;
}
});
});
});
});