2017-05-19 10 views
1

インクリメントおよびデクリメントボタンと同期する価格カウンタを実行しようとしていますが、ボタン(+/-)のいずれかをクリックしても価格が変化しません。私はこの問題を解決できますか?ありがとう!!!変更時のインクリメントおよびデクリメント価格の値

$('#plus').click(function add() { 
 
    var $qtde = $("#quantity"); 
 
    var a = $qtde.val(); 
 
    
 
    a++; 
 
    $("#minus").attr("disabled", !a); 
 
    $qtde.val(a); 
 
}); 
 
$("#minus").attr("disabled", !$("#quantity").val()); 
 

 
$('#minus').click(function minust() { 
 
    var $qtde = $("#quantity"); 
 
    var b = $qtde.val(); 
 
    if (b >= 1) { 
 
     b--; 
 
     $qtde.val(b); 
 
    } 
 
    else { 
 
     $("#minus").attr("disabled", true); 
 
    } 
 
}); 
 

 
/* On change */ 
 
$(document).ready(function() 
 
{ 
 
    function updatePrice() 
 
    { 
 
     var price = parseFloat($("#quantity").val()); 
 
     var total = (price + 1) * 1.05; 
 
     var total = total.toFixed(2); 
 
     $("#total-price").val(total); 
 
    } 
 
    $(document).on("change, keyup, focus", "#quantity", updatePrice); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="button" value="-" id="minus" /> 
 
<input type="text" id="quantity" value="" name="quantity" /> 
 
<input type="button" value="+" id="plus" /> 
 
<br /> 
 
<input id="total-price" readonly="readonly" value=""/>

+0

入力に論理的な変更は、変更イベントをトリガしません。プラスまたはマイナスをクリックしても、数量を変更するときに変更イベントがトリガーされません。あなたは論理的に量値を変更した後にupdatePriceを呼び出すだけです。これは、他のバインディングとは異なり、ドキュメント内でupdatePriceスコープがどのように設定されているかで問題になります。 – Taplar

答えて

1

あなたが入力のクリックがあるたびに、あなたが期待している動作を取得します更新するバインディングを変更した場合。

$('#plus').click(function add() { 
 
    var $qtde = $("#quantity"); 
 
    var a = $qtde.val(); 
 

 
    a++; 
 
    $("#minus").attr("disabled", !a); 
 
    $qtde.val(a); 
 
}); 
 
$("#minus").attr("disabled", !$("#quantity").val()); 
 

 
$('#minus').click(function minust() { 
 
    var $qtde = $("#quantity"); 
 
    var b = $qtde.val(); 
 
    if (b >= 1) { 
 
    b--; 
 
    $qtde.val(b); 
 
    } else { 
 
    $("#minus").attr("disabled", true); 
 
    } 
 
}); 
 

 
/* On change */ 
 
$(document).ready(function() { 
 
    function updatePrice() { 
 
    var price = parseFloat($("#quantity").val()); 
 
    var total = (price + 1) * 1.05; 
 
    var total = total.toFixed(2); 
 
    $("#total-price").val(total); 
 
    } 
 
    // On the click of an input, update the price 
 
    $(document).on("click", "input", updatePrice); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="button" value="-" id="minus" /> 
 
<input type="text" id="quantity" value="" name="quantity" /> 
 
<input type="button" value="+" id="plus" /> 
 
<br /> 
 
<input id="total-price" readonly="readonly" value="" />

関連する問題