繁体   English   中英

将用户输入分配给索引中具有变量的对象

[英]Assigning user input to object with variable in its index

我正在尝试使用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)

好,对于初学者来说,

$('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] );
    });
});

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

较早之前的无用道歉,现在是凌晨5点,显然我有点妄想。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM