如何在Javascript方程中引用表单字段
How do I reference a form field in a Javascript equation?
我有这个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;
}
这是我创建的表单字段,我想把它拉到等式中:
<input name="yellowtape" type="text" id="yellowtape" style="width:243px;">
我认为通过添加:
var withDiscount = total + yellowtape - min ;
它会工作,但我不明白。我需要将他们在表单字段'yellowtape'中输入的任何数字添加到var withDiscount。
yellowtape
为DOM元素。你需要取它的值,并确保它是一个数字:
var yellowtape = parseFloat(document.getElementById('yellowtape').value);
var withDiscount = total + yellowtape - min ;
如果您先存储输入值,您所拥有的将会工作:
var yellowtape = document.getElementById('yellowtape').value;
var withDiscount = total + yellowtape - min ;
相关文章:
- 在表单提交按钮的点击事件中调用函数时发生引用错误
- 如何在加载表单时获得对Builder配置的Jenkins果冻脚本中表单元素的引用
- 使用ajax在coldfusion中引用表单值
- Dynamics CRM 2015:如何获得我创建的Web资源按钮,以引用我添加到表单中的Javascript库中的函数
- 如何在表单中引用跨度元素
- 表单验证返回引用错误
- CRM 2013-引用子记录的Javascript表单警报(onload事件)
- 使用javascript引用表单元素
- 引用在表单容器中为空
- 从模块内部引用特定的表单元素
- 页面重定向,在表单提交时传递引用 URL
- ExtJS MVC 表单引用
- 引用分配给 jQUERY 中变量的表单
- 如何在 Meteor 中添加与自动表单的关系或引用
- 在输入表单标记中使用 onclick 属性时出现 JavaScript 未捕获的引用错误
- AngularJS验证引用具有变量名称的表单对象
- 在iFrame(不同域)表单隐藏字段中抓取父级引用
- Javascript引用表单,更改action url,然后http get
- Angularjs在控制器中引用表单,获取html元素
- jQuery表单提交按引用表单