Javascript/Jquery - 多个更改(函数()

Bra*_*lly 6 javascript jquery

我对Jquery/JS有点新意见并经过一些帮助.我意识到希望这是一个更好的方法来做到这一点,任何帮助将不胜感激.

我正在构建一个可以计算总数的表单.那是:

'成本'x'(1 +%(%))'='总成本'

我有很好的工作,虽然我想这样做,如果我改变' 百分比 '字段,或' 成本 '字段,然后总量更新.目前只有更新" 费用 "才会更新总数.

希望有道理.代码如下.

$(document).ready(function(){

$("#cost").change(function() { 
var findprofit = parseFloat($("#cost").val());
var profit = (findprofit*.01)
var margin = parseFloat($("#percentage").val());
var total = profit*margin;
$(".profit_1_1").html(total);
var totalprofit = (total + findprofit);
$("#total").val(totalprofit);
});

<input type="text" name="cost" id="cost"  />
<input type="text" name="percentage" id="cost" percentage />
<input type="text" name="total" id="total" readonly />
Run Code Online (Sandbox Code Playgroud)

Jam*_*lly 17

$("#cost, #percent").change(function() { 
    ...
});
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩.

编辑:您需要为百分比输入提供"百分比"的ID.两个元素可能没有相同的ID.

<input type="text" name="cost" id="cost"  />
<input type="text" name="percentage" id="percent" percentage />
<input type="text" name="total" id="total" readonly />
Run Code Online (Sandbox Code Playgroud)

此外,我不确定输入结束时"百分比"是什么.百分比不是有效属性.