0

我正在尝试使用 JS 来获取用户输入并根据用户的输入修改某些对象属性。我将对象的索引存储在选择的 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)
4

1 回答 1

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). 事件侦听器应该添加到$.each函数外部,$.getJson调用内部,它需要遍历elements对象,并尝试找到要更新的正确数据。

为早些时候的无用道歉,现在是凌晨 5 点,显然我有点妄想。

于 2012-06-18T20:37:53.557 回答