2016-08-13 19 views
-5

我正在製作一個表單,其中有一個字段用於輸入項目的數量。Javascript中的實時計算:基於項目的文本字段和固定價格比率

我想根據固定價格和數量計算物品的成本。

我想知道如何通過javascript實現它。

​​

我想舉辦每個項目的價格在一個陣列 - 爲以後增加更多產品的緣故。

請幫助我,參考下面的圖片瞭解我在問什麼。我真的很抱歉我的英文不好。

+0

https://developer.mozilla.org/en-US/docs/Web/Events/input – Teemu

+1

@Teemu - 我想https://developer.mozilla.org/en-US/docs/Web/JavaScript首先 –

+0

@Teemu謝謝:) – dragonball

回答

0
element.onkeypress = function() { // element should be your input field 
    var value = this.value; // get the value of the field 
    value *= cost; // multiply value by cost and save result; change cost to suit your needs 
    // set outputelement to the element where you show the result 
    outputelement.innerHTML = value; // set the text of the output field to the result; 
}