Вопрос

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

<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

$('.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.

Demo.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top