简体   繁体   English

试图从数量和价格中获得总价值

[英]Trying to get the total to generate from quantity and price

I've been working on this for a little longer then needed, but I can't find what is going wrong here. 我已经花了比需要的时间更长的时间,但是我找不到这里出了什么问题。 I think I've looked at it way too long. 我想我看了太久了。 I need to get the items to add up after quantity is add to the form. 将数量添加到表单后,我需要获取要累加的项目。

Here is my HTML 这是我的HTML

<div class="row-fluid container-cart">

    <div class="span4">
        <div class="thumbnail">
            <img src="http://myurl.com/image.jpg" />
            <div class="caption">
                <h3 class="">Title</h3>
                <p class="">Description</p>
                <div class="row-fluid">
                    <div class="span6">
                        <p class="lead">
                            <span id="item-price">100</span>
                            <span class="price-integer">
                                <input type="hidden" value="100">
                            </span>
                        </p>
                    </div>
                    <div class="span6">
                        <span>Quantity</span>
                        <input type="text" name="" id="quantity" class="stored quantity" autocomplete="off" value="">
                    </div>
                    <div class="span6">
                        <input type="text" name="base-total" id="base-total" class="stored readonly base-total" value="" readonly>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="span6">
    Total: 
    <span class="total-cart">
         <input type="text" name="total-cart" id="total-cart" class="stored readonly" value="" disabled="disabled">
    </span>
</div>

Here is my JS 这是我的JS

$('.quantity').on('keyup', function() {
    var sum = 0;
    $(".container-cart").each(function(i,o){

        total = parseInt($(o).find(".quantity input").val(), 10) * parseInt($(o).find(".price-integer input").val(), 10);
        if(!isNaN(total) /*&& total.length!=0**/) {
            $(o).find(".base-total").val(total);
            sum += total;
        }
    });
    $("#total-cart").val(sum);
});

Looks like you typed a wrong selector, it's the .quantity input , the .quantity is an input, so it won't have any children, maybe you want to mean the input.quantity which means finding the input having class quantity . 看起来您输入了错误的选择器,它是.quantity input.quantity是输入,因此它没有任何子代,也许您想表达的是input.quantity ,这意味着找到具有类quantityinput

Demo. 演示

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

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