0

此代码块从 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;
            }
          });
      });
   });
});
4

2 回答 2

1

您需要在更改函数之外声明 DeliveryMethod:

var XMLObject = {};
var DeliveryMethod;

$('<div class="options">Delivery: <select id="Delivery_Method"><option value="100" selected="selected">US Mail</option><option value="300">Foreign Mail</option><option value="400">UPS Priority - 10am guarantee</option></select></div>').appendTo(newDiv);

 $('#Delivery_Method').change(function(){
   DeliveryMethod = $('select#Delivery_Method').val(); 
   });

    XMLObject.deliveryMethod = DeliveryMethod;
    console.log(XMLObject);
于 2012-05-29T23:40:42.260 回答
0

Firefox 控制台不会根据 Jquery 更改事件进行更新。因此,通过打印到 html 来测试它给了我想要的结果。

于 2012-05-30T17:19:14.390 回答