将用户输入分配给其索引中包含变量的对象

时间:2012-06-18 15:53:06

标签: javascript json

我正在尝试使用JS来获取用户输入并根据用户的输入修改某些对象属性。我将对象的索引存储在select的alt属性中,以便使用它来更新正确的对象。我收到错误:元素[Payment_Format_name]未定义

WF.php文件从CSV中获取数据并将其格式化为覆盖维对象。

$(document).ready(function() { 
$.getJSON('WF.php', function(data) {

    var newDiv, NewDiv2, NewDiv3,InvoiceInfo, count, DeliveryMethod, PaymentFormat, Payment_Format_id, Payment_Format_name;

    count = 0;

    $.each(data, function(index, element) {
        count = count + 1; 
        //document.write (count);   

        newDiv = $('<div/>').addClass('row').appendTo('#showdata');

        newDiv3 = $('<div/>').addClass('hd').appendTo(newDiv);
        $('<div class="hd_field">' + element['PmtRec']['RcvrParty']['Name']['Name1'] + '</div>').appendTo(newDiv3);

        if (element['PmtRec']['PmtMethod'] === 'CHK'){

                $('<div class="hd_field">Delivery Method: <select alt="Delivery_Method" " id="Delivery' + count  +'" class="Delivery_Method"><option value="100" selected="selected">US Mail</option><option value="300">Foreign Mail</option><option value="J00">Certified Mail with Return Receipt</option></select><div id="Selected_Method' + count +'"></div></div>').appendTo(newDiv3);
        }
         else if (element['PmtRec']['PmtMethod'] === 'DAC') {
                $('<div class="hd_field">Payment Format: <select alt="'+index +'" id="Payment_' + count  +'" class="Payment_Format"><option value="CTX" selected="selected">Company to Company</option><option value="PPD">Company to Person</option></select><div id="Selected_Format'+count+'"></div></div>').appendTo(newDiv3);

         }
        $('<div class="hd_field">' + 'Total: ' + element['PmtRec']['CurAmt'] + '</div>').appendTo(newDiv3);

        InvoiceInfo = element['PmtRec']['PmtDetail']['InvoiceInfo'];

        $.each(InvoiceInfo, function(index, element) {
        newDiv2 = $('<div/>').addClass('sub_row').appendTo(newDiv);
                $('<div class="field">' + element['InvoiceNum'] + '</div>').appendTo(newDiv2);
                $('<div class="field">' + element['NetCurAmt'] + '</div>').appendTo(newDiv2);
            });

        $('select.Payment_Format').change(function(){ 
            Payment_Format_id = ($(this).attr('id').match(/[\d]+$/));
            Payment_Format_name = ($(this).attr('alt')); 
            //alert(Payment_Format_name);
            PaymentFormat = ($(this).val()); 
            element[Payment_Format_name] = Payment_Format_name;
            element[Payment_Format_name]['PmtRec']['PmtFormat'] = PaymentFormat;
            $('#Selected_Format' + Payment_Format_id).text('Selected Format: ' + element[Payment_Format] );

            });

        });
            console.log(data);
     });

});

PHP(这是一个片段,我实际上在这里创建了更多元素)

     if (($handle = fopen('upload/BEN-new.csv', "r")) === FALSE) {
            die('Error opening file'); 
         }

         $headers = fgetcsv($handle, 1024, ',');
         $cardCodes = array();
         $payments = array();
         $details = array ();

        while ($row = fgetcsv($handle, 1024, ",")) {
               $cardCodes[] = array_combine($headers, $row);

        }

            $prevCode = '';

            foreach ($cardCodes as $key => $value) {

                $payments[$value['CardCode']]['PmtRec']['PmtCrDr'] = 'C';
                $payments[$value['CardCode']]['PmtRec']['PmtFormat'] = 'CTX';
                                    fclose($handle);
                                    echo json_encode($payments)

1 个答案:

答案 0 :(得分:1)

好的,对初学者来说,

$('select.Payment_Format').change(function(){ 
        Payment_Format_id = ($(this).attr('id').match(/[\d]+$/));
        Payment_Format_name = ($(this).attr('alt')); 
        PaymentFormat = ($(this).val()); 
        element[Payment_Format_name] = Payment_Format_name;
        element[Payment_Format_name]['PmtRec']['PmtFormat'] = PaymentFormat;
        $('#Selected_Format' + Payment_Format_id).text('Selected Format: ' + element[Payment_Format] );
    });
});

不是您想要的 - 对于'select.Payment_Fomat'的每次迭代,此函数将重新分配给$.each(data, function(index, element)元素的change事件。应该在$.each调用内部$.getJson函数之外添加事件侦听器,它需要遍历elements对象,并尝试查找要更新的正确数据。

早些时候对无用的道歉,早上5点,显然我有点妄想。