0

JSを使用してユーザー入力を取得し、ユーザー入力に基づいて特定のオブジェクト属性を変更しようとしています。オブジェクトのインデックスをselectのalt属性に保存して、それを使用して正しいオブジェクトを更新しています。エラーが発生しました:element[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 に答える