如何在Javascript公式中引用表单字段?

如何在Javascript公式中引用表单字段?,javascript,forms,Javascript,Forms,我有一个JavaScript,它将一系列数字相加,并创建一个总数。然后从这一系列数字中找出最低的一个,然后从总数中减去它。我希望用户能够输入一个数值,并将该数值作为等式的一部分。首先我会给你我的代码,然后我会告诉你我尝试了什么 这是执行加法和减法运算的脚本: var prices = []; function remove(arr,itm){ var indx = arr.indexOf(itm); if (indx !== -1){ arr.splice(indx,1);

我有一个JavaScript,它将一系列数字相加,并创建一个总数。然后从这一系列数字中找出最低的一个,然后从总数中减去它。我希望用户能够输入一个数值,并将该数值作为等式的一部分。首先我会给你我的代码,然后我会告诉你我尝试了什么

这是执行加法和减法运算的脚本:

var prices = [];
function remove(arr,itm){
  var indx = arr.indexOf(itm);
  if (indx !== -1){
    arr.splice(indx,1);
  }
}

function calculateSectedDues(checkbox, amount) {
  if (checkbox.checked === true) {
    prices.push(amount);
  } else {
    remove(prices, amount);
  }
  var total = 0;
  for (var i = 0, len = prices.length; i < len; i++)
    total += prices[i];

  var min = prices.slice().sort(function(a,b){return a-b})[0];
  if(typeof min === 'undefined') min = 0;
  var withDiscount = total - min;
  var discountAmount = withDiscount - total;
  document.querySelector("#total").innerHTML = total;
  document.querySelector("#discount").innerHTML = discountAmount;
  document.querySelector("#newTotal").innerHTML = withDiscount;
  document.getElementById("FinalTotal").value = withDiscount;
}

这会管用的,但我想不出来。我需要将他们在表单字段“yellowtape”中输入的任何数字添加到var withDiscount。

如果您首先存储输入值,您拥有的将起作用:

var yellowtape = document.getElementById('yellowtape').value;
var withDiscount = total + yellowtape - min ;

yellowtapel
是DOM元素。您需要获取它的值并确保它是一个数字:

var yellowtape = parseFloat(document.getElementById('yellowtape').value);
var withDiscount = total + yellowtape - min ;

使用
document.getElementById(“yellowtape”).value
,而不是只使用
yellowtape
,它可以完美地工作,只有一个小的例外。它将自己添加到所有数字的数组中,并减去它本身,因为它会认为它是数组中最小的数字之一。我希望它是单独的,没有例外,它将自己加入到任何总数中。另一个词yellowtape在这个场景中也可以是min,但我不希望它是min。
var yellowtape = document.getElementById('yellowtape').value;
var withDiscount = total + yellowtape - min ;
var yellowtape = parseFloat(document.getElementById('yellowtape').value);
var withDiscount = total + yellowtape - min ;