简体   繁体   English

如何在动态列表中添加多个数字?

[英]How to add multiple numbers in dynamic list?

So I've been searching and re-searching a lot on this and seem to be getting nowhere. 因此,我一直在搜索和重新搜索很多内容,但似乎一无所获。

I have code that has a big list of data and when I click on an item the item forms a new list with a price. 我的代码中包含大量数据,当我单击某个项目时,该项目会形成一个新的价格列表。 I'm trying to get the prices on the new list to form a total and I'm not very good with jQuery yet so I can only get so far as to display the first list items price and I am not sure how I can get it to loop through all of my .textbox 's to add all the numbers not just the first one only. 我正在尝试获取新列表上的价格以形成总计,但我对jQuery不太满意,所以我只能显示第一个列表中的价格,而且不确定如何获取它遍历我所有的.textbox以添加所有数字,而不仅仅是第一个。

So here is what I have so far: 所以这是我到目前为止所拥有的:

$('.itemproduct').click(function () {
    var product = $(this).find('.product').html();
    var price = $(this).find('.price').html();
    $("#list").append('<tr id="help">' + '<td>' + product + '</td>' + '<td class="move add">' + '<input type="text" class="textbox" value="' + price + '"/>' + '</td>' + '</tr>');
    var num = parseFloat($(".textbox").val());
    var new_num = $("#total").val(num.toFixed(2));
});

JSFiddle JSFiddle

You need to add a variable that tracks the total, and iterate the textboxes to calculate the total, and then display the total in the '#total' box. 您需要添加一个跟踪总数的变量,并迭代文本框以计算总数,然后在“ #total”框中显示总数。 As such: 因此:

Working fiddle: http://jsfiddle.net/8d74n/4/ 工作提琴: http : //jsfiddle.net/8d74n/4/

$('.itemproduct').click(function () 
{
    var product = $(this).find('.product').html();
    var price = $(this).find('.price').html();
    var total = 0.00;

    $("#list").append('<tr id="help">' + '<td>' + product + '</td>' + '<td class="move add">' + '<input type="text" class="textbox" value="' + price + '"/>' + '</td>' + '</tr>');

    $( '.textbox' ).each( function( ) 
    {
        var itemPrice = parseFloat( $( this ).val( ) );
        total += itemPrice;
    });

    total = total.toFixed( 2 );
    $( '#total' ).val( total );    
});

EDIT ----------- 编辑-----------

Was just fooling around with the fiddle and noticed that I got a rounding error, the total started to become numbers like '6.58999999999999999996' To account for this I added the line 'total = total.toFixed( 2 ); 只是在弄弄小提琴,发现我遇到了舍入错误,总数开始变成像'6.589999999999999999999996'这样的数字。为此,我添加了行'total = total.toFixed(2);。 also updated the fiddle version 还更新了小提琴版本

Or try this: 或尝试以下方法:

$('.itemproduct').click(function () {

    var product = $(this).find('.product').html();
    var price = $(this).find('.price').html();

    $("#list").append('<tr id="help">' + '<td>' + product + '</td>' + '<td class="move add">' + '<input type="text" class="textbox" value="' + price + '"/>' + '</td>' + '</tr>');

    var num = parseFloat($(".textbox").val());

    createTotal();    

});

function createTotal(){   
    var total = 0.0;
    $('#list tr').each(function(key, val){        
        total += parseFloat($(val).find('input').val());
    });
    $("#total").val(total.toFixed(2));
}

http://jsfiddle.net/8d74n/5/ http://jsfiddle.net/8d74n/5/

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

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