0

私は基本的な注文フォーム(テスト目的でのみ4つの入力)を持っています。値が入力され、0より大きい入力(合計アイテム)の数をカウントしたい(基本的に、注文された製品の数、数量と混同しないでください) . 製品を追加しても問題ありませんが、アイテムを削除すると問題が発生します(アイテムを0に設定します)。それを解決するのを手伝ってくれませんか。

jsfiddle の作業: http://jsfiddle.net/nitadesign/97tnrepg/33/

そして、適切な場所に注意を向けるためのいくつかの行:

function GetOrder(curId){
var order = null;

for(i = 0; i< orders.length; i++){
     if(orders[i].id == curId){
        order = orders[i];
        break;
    }
}

return order;
}

function CalculateTotal(){
var total = 0;
for(i = 0; i< orders.length; i++){
    total = total + orders[i].packTotal;
}
console.log(total);

if(total > 0){
    $("#order_total").html('Total Items:' + i + '<br>' + 'Order Subtotal: ' + total);
    $("#order_total").show();
    $('.submitorder').show();
}
if(total == 0){
    $("#order_total").html('Your shopping basket is empty');
    $("#order_total").show();
    $('.submitorder').hide();
}
}

事前にご協力いただきありがとうございます。

4

2 に答える 2

2

jqueryセレクターを使用してループするだけです。

$("input").change(function(){
    var counter = 0;
    $("input").each(function(){
        if($(this).val() != "" && $(this).val() != 0) counter++;
    });

    $("#order_total").html('Total Items:' + counter + '<br>' + 'Order Subtotal: ' + total);
});
于 2015-09-02T15:27:01.020 に答える
0
if(total > 0){
    var counter = 0;  
    $("input[type=text]").each(function(){
    if($(this).val() != "" && $(this).val() != 0) counter++;
    });
    $("#order_total").html('Total Items:' + counter + '<br>' + 'Order Subtotal: ' + total);
    $("#order_total").show();
    $('.submitorder').show();
}
if(total == 0){
    $("#order_total").html('Your shopping basket is empty');
    $("#order_total").show();
    $('.submitorder').hide();
}
于 2015-09-02T16:05:05.353 に答える