编辑脚本以包括订单总计复选框
Edit a script to include checkbox to order total
我有这个脚本:
//CODE BELOW THIS POINT IS NOT MINE//
/* This script is Copyright (c) Paul McFedries and
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as
this Copyright notice remains in place.*/
function CalculateTotal(frm) {
var order_total = 0
// Run through all the form fields
for (var i=0; i < frm.elements.length; ++i) {
// Get the current field
form_field = frm.elements[i]
// Get the field's name
form_name = form_field.name
// Is it a "product" field?
if (form_name.substring(0,4) == "PROD") {
// If so, extract the price from the name
item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))
// Get the quantity
item_quantity = parseInt(form_field.value)
// Update the order total
if (item_quantity >= 0) {
order_total += item_quantity * item_price
//My attempt at updating order total for checkbox
if(document.checkbox.select[i].checked)
{
order_total = item_price
}
}
}
}
// Display the total rounded to two decimal places
frm.TOTAL.value = round_decimals(order_total, 2)
}
function round_decimals(original_number, decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(result1)
var result3 = result2 / Math.pow(10, decimals)
return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
// Convert the number to a string
var value_string = rounded_value.toString()
// Locate the decimal point
var decimal_location = value_string.indexOf(".")
// Is there a decimal point?
if (decimal_location == -1) {
// If no, then all decimal places will be padded with 0s
decimal_part_length = 0
// If decimal_places is greater than zero, tack on a decimal point
value_string += decimal_places > 0 ? "." : ""
}
else {
// If yes, then only the extra decimal places will be padded with 0s
decimal_part_length = value_string.length - decimal_location - 1
}
// Calculate the number of decimal places that need to be padded with 0s
var pad_total = decimal_places - decimal_part_length
if (pad_total > 0) {
// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}
//CODE ABOVE THIS POINT IS NOT MINE//
你定义一个项目的价格,它的ID是这样的:
<input type="text" name="PROD_CH_35.00" onChange="CalculateTotal(this.form)">
这是复选框功能代码:
function totalcheckbox() {
document.checkbox.TOTAL.value = ''; //I set the value of all the checkboxes equal to nothing
var total = 0;
//For loop iterates through all the checkboxes. ++ adds one each time to i.
for (i=0;i<document.checkbox.select.length;i++) {
if (document.checkbox.select[i].checked) {
total = total + parseInt(document.checkbox.select[i].value); //take whatever they selected and make it into an integer
}
}
document.checkbox.TOTAL.value = total; //total value box equal to the sum
}
我的复选框是这样的:
<input type="checkbox" name="select" value="15" onchange="totalcheckbox()">
我遇到的问题是其中两个字段是复选框。我想要它,所以当我选中这些复选框时,它将添加到订单总额中。当我取消选中复选框时,它将减去复选框的价格。
例如,我可以有一个这样的复选框:
<input type="checkbox" name="PROD_CH_15.00" value="15" onchange="totalcheckbox()">
复选框的函数本身可以添加总计,订单总计函数也可以工作。最大的问题是编辑订单总计脚本,以便复选框将更改总价,就像您在文本框中输入数量时一样,它将更新总计。
这是工作复选框代码的JS小提琴:http://jsfiddle.net/7uzr4/
这是订单总代码的JS小提琴:http://jsfiddle.net/X2wMR/
如何组合这两个代码,以便复选框代码也添加到订单总额中?
您必须根据字段类型设置item_quantity
。您当前的CalculateFunction
只需稍作更改即可使用:
// Get the quantity
if(form_field.type == 'checkbox') {
// Will be 0 for unchecked, 1 for checked
item_quantity = form_field.checked;
} else {
item_quantity = parseInt(form_field.value, 10);
}
然后只需从复选框的onchange
调用相同的函数:
<input type="checkbox" name="PROD_CH_15.00" value="15" onchange="CalculateTotal(this.form)">
jsfiddle 上的工作版本
相关文章:
- 如何在单击复选框后调用控制器方法
- 在GridView中,当单击复选框时,在网格的同一行中使用JavaScript将标签中的值添加到TextBox
- 在网页中单击复选框
- jquery表单验证复选框错误显示
- 单击复选框时显示日期选择器
- 选择全部单击复选框以及全部拒绝
- 在 Telerik RadGrid 中单击复选框时打开对话框窗口
- 在JS中单击复选框时显示总价
- 单击复选框时重定向
- 如何在单击复选框按钮时制作单选按钮
- 如何使用角度指令控制器动态单击复选框
- 如何通过单击复选框来显示或隐藏网络图表
- 单击复选框时的角度显示一个元素
- 复选框不能通过直接单击复选框来选中或取消选中
- 单击复选框时将此元素添加到收藏夹数组 + 后 Ajax 元素关系
- 单击复选框后未收到复选框事件
- 单击复选框时汇总隐藏字段的值不起作用
- 如何使文本输入框在单击复选框时变为不可写
- AngularJS不保留表单中复选框列表中的选定项
- 在“文档.表单 1.复选框.选中 == 真”