2017-07-14 94 views
0

好的...因爲我是JS的新手,所以不知道如何擺脫這個問題。這是我的形式enter image description here通過輸入Jquery計算加/減

我想抓住瓶子的總安全。

總安全 =數量* 500

其工作正常,但是當我在空瓶輸入數的計算不符合。應當減去總安全量(空瓶子)。我無法做到這一點..

這是我的代碼;

(function() { 
     $('input[name="quantity"]').bind("focus blur change keyup", function() 
      $('input[name="total_amount"]').val($(this).val() * $("#pricevalue").val()); 
      $('input[name="total_security"]').val($(this).val() * 500); 
         }); 
       })(); 

The above code is working fine... code below is creating the problem 

     (function() { 

      $('input[name="empty_bottles"]').bind("on keyup", function() { 

          var value = $(this).val() * 500; 

          var test = $("#total_security").val() - value; 
          $("#total_security").val(test); 

         }); 
       })(); 

在此先感謝

+0

您不適合是什麼意思? – error404

+0

第一次減去值,但當我退格並輸入新值時,它從它最後一個值中減去。沒有得到應該是數量* 500的值。 – Coffee

+2

當一半人失蹤時很難遵循邏輯。你可以編輯問題,包括HTML和JS –

回答

0

從我所收集:

  1. 共安全性=(數量* 500) - 瓶
  2. 總金額=數量*價格

如果這是正確的,那麼下面的代碼應該可以工作。

$(document).on('input change',':input',function() { 
    var quantity = $('[name="quantity"]').val() || 0; 
    var price = $("#pricevalue").val() || 0; 
    var bottles = $('[name="empty_bottles"]').val() || 0; 
    $('[name="total_security"]').val((quantity * 500) - bottles) 
    $('[name="total_amount"]').val(quantity * price); 
}); 

例子:https://jsfiddle.net/mou9Ldmu/2/