从输入字段计算2个值并将其显示在标签中

时间:2019-04-10 18:37:11

标签: javascript jquery

我正在尝试执行以下操作。取一个输入字段,将其添加到另一个字段,然后在表单上的标签中显示它。我以前在这里添加了2个值,然后在输入中显示它们,但是不再需要了。这是代码。我假设我会附加标签以显示新的总计,但是我无法使其正常工作。

var $recurringCostField = $('#am_attribute_campaign_addon_monthly_cost_value');
var $recurringTotalCostField = $('#am_attribute_campaign_addon_total_monthly_cost_value');
var $totalRetailAmountField = $('#am_oie_string_total_monthly_cost_value').closest('.field');
var $totalRetailAmountFieldLabel= $totalRetailAmountField.find('label');
var $oneTimeCostField = $('#am_attribute_campaign_addon_one_time_cost_value');

function calcTotalRetailVal() {
    var num1 = $oneTimeCostField.val();
    var num2 = $recurringTotalCostField.val();
    var result = parseFloat(num1.replace(/,/g, '')) + parseFloat(num2.replace(/,/g, ''));
    if (!isNaN(result)) {
        $totalRetailAmountFieldLabel.val(result.toFixed(2));
     //Append here?
    }   
}
    calcTotalRetailVal();
      $oneTimeCostField.on("keydown keyup", function() {
        calcTotalRetailVal();
    });
      $recurringTotalCostField.on("keydown keyup", function() {
        calcTotalRetailVal();
    });

0 个答案:

没有答案