总和无法添加和删除动态字段

时间:2013-05-07 14:15:40

标签: php jquery autocomplete dynamic-data

我有一个调用数据库来获取自动填充数据的脚本。

一旦选择了特定的自动填充数据(项目名称),就会填充一些其他字段,即:项目代码,价格&量。

当数量更改时,行总计字段将更改为“数量*价格”总计。

可以选择以相同的方式动态添加行并自动完成每个行...有一个总计将每行总计加在一起。

我的问题是,总数只会在数量发生变化时发生变化,它应该加载在每一行的价格焦点上 - 同样,删除一条线并不会从总计中删除那些特定的线路总额而且如果你更改第一行的数量(只有第一行受到影响)它会将总计重新设置为该行现已添加的任何内容...请有人查看我的代码。

现场版是 http://cardoso.co.za/form/

如果您浏览,也可以在那里获取文件 http://cardoso.co.za/form/form.zip

非常感谢任何和所有帮助!!!

在这里编辑整个脚本中的一些代码:

    var $itemsTable = $('#itemsTable');
var rowTemp = [
'<tr class="item-row">',
'<td><a id="deleteRow"><img src="images/icon-minus.png" alt="Remove Item" title="Remove Item"></a></td>',
'<td><input name="itemType" class="itemType" id="itemType" tabindex="1" style="width:350px;"/></td>',
'<td align="center"><input name="itemCode" class="itemCode" id="itemCode" readonly="readonly" style="width:60px;" tabindex="-1"/></td>',
'<td align="center"><input name="itemQty" class="itemQty" id="itemQty" tabindex="2" style="width:40px;" maxlength="4" value=""/></td>',
'<td width="14%" align="center"><input name="itemPrice" class="itemPrice" id="itemPrice" readonly tabindex="-1"/></td>',
'<td width="17%" align="right"><input name="itemTotal" class="itemTotal" id="itemTotal" readonly tabindex="-1"/></td>',
'</tr>'
].join('');
$('#addRow').bind('click',function(){
var $row = $(rowTemp);
var $itemType = $row.find('#itemType');
var $itemCode = $row.find('.itemCode');
var $itemPrice = $row.find('.itemPrice');
var $itemQty = $row.find('.itemQty');
var $itemTotal = $row.find('.itemTotal');
if ( $('#itemType:last').val() !== '' ) {
$row.find('#itemType').autocomplete({
source: 'item-data.php',
minLength: 1,
select: function(event, ui) {
$itemType.val(ui.item.itemType);
$itemCode.val(ui.item.itemCode);
$itemPrice.val(ui.item.itemPrice);
$itemTotal.focus().val(ui.item.itemPrice); 
$itemQty.focus().val(1);
$itemQty.keyup(function() {
var Quantity = $itemQty.val();
var Prices = $itemPrice.val();
var ItemsTotal = Quantity * Prices;
$itemTotal.val(ItemsTotal.toFixed(2));
var Tsum = 0;
$('.itemTotal').each(function() {
if(!isNaN(this.value) && this.value.length!=0) {
Tsum += parseFloat(this.value);
$('#toTally').val(Tsum.toFixed(2))
}
}); 
});
return false;
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.itemType + "</a>" )
.appendTo( ul );
};
$('.item-row:last', $itemsTable).after($row); 
$('#minusRow').show();
$('#resetTable').show();
$($itemType).focus();
}
return false;
});

我已经实现了数组,很快就会发布这个数据,一旦我让它与ID和Classes一起工作而没有干扰。

1 个答案:

答案 0 :(得分:0)

我通过以下方式解决了这个问题:

            ( function($) {
        var pounter = 1;
        $(document).ready(function(){
            var qounter = 1;
            var trounter = 1;

                //Count each instance and add number to Name
                $row.find('._ext_price_total_html').each(function() {
                $(this).attr({
                'name': function(_, name) {
                    return name + pounter },
                    });
                pounter++;
                })
            $row.find('.quantitys').each(function() {
                $(this).attr({
                'name': function(_, name) {
                    return name + qounter },
                    });
                qounter++;
                })  
            $('#quantity', function(){
                var Tsum = 0;
                $('[id^="_ext_price_total_html"]').each(function() {
                    if(!isNaN(this.value) && this.value.length!=0) {
                    Tsum += parseFloat(this.value);
                    }
                        $('.toTally').val(Tsum.toFixed(2))                                                          
                })
                })
                $('#quantity').change(function(){
                var Tsum = 0;
                $('[id^="_ext_price_total_html"]').each(function() {
                    if(!isNaN(this.value) && this.value.length!=0) {
                    Tsum += parseFloat(this.value);
                    }
                        $('.toTally').val(Tsum.toFixed(2))                              
                    })
                })
                $('#quantity').keyup(function(){
                var Tsum = 0;
                $('[id^="_ext_price_total_html"]').each(function() {
                    if(!isNaN(this.value) && this.value.length!=0) {
                    Tsum += parseFloat(this.value);
                    }
                        $('.toTally').val(Tsum.toFixed(2))
                })
                })
        //adjust/subtract from total when removeing row 

        $('#deleteRow').live('click',function(){
var delAsk = confirm('Remove this item?');
        if (delAsk)
        {
        $(this).parents('.jshop_prod_cart').remove();
            var Tsum = 0;
                $('[id^="_ext_price_total_html"]').each(function() {
                    if(!isNaN(this.value) && this.value.length!=0) {
                    Tsum += parseFloat(this.value);
                    }
                        $('.toTally').val(Tsum.toFixed(2))

                })
        }
        else
        {
            ('cancel');
            }
                });