修改 (jQuery) JS 以包含代码来跟踪复选框和下拉列表上所做的更改并更新价格
-
18-09-2019 - |
题
我有一个 javascript 代码,我正在使用 jQuery Calculation 插件(可以从 链接文本)。我的表格有点像购物车表格,即价格根据输入的数量更新。该表格具有接受数字的文本框,这表示数量。输入/修改此数量后,该数量的价格就会更新,同时总计也会更新。目前,它只允许我使用文本框。我希望能够在表单中使用复选框和下拉列表,并希望 JS 代码能够处理更新价格和总计并立即显示(就像当前现有文本框的情况一样)。我在本地主机上尝试了此操作,但我已经结束了当前功能的破坏,或者更新的代码不适用于复选框和下拉列表。
到目前为止我的表格是:
<script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="js/jquery.calculation.js" type="text/javascript"></script>
<SCRIPT type="text/javascript">
var bIsFirebugReady = (!!window.console && !!window.console.log);
$(document).ready(
function (){
// update the plug-in version
$("#idPluginVersion").text($.Calculation.version);
// bind the recalc function to the quantity fields
$("input[name^=qty_item_]").bind("keyup", recalc);
// run the calculation function now
recalc();
// automatically update the "#totalSum" field every time
// the values are changes via the keyup event
$("input[name^=sum]").sum("keyup", "#totalSum");
// automatically update the "#totalAvg" field every time
// the values are changes via the keyup event
$("input[name^=avg]").avg({
bind:"keyup"
, selector: "#totalAvg"
// if an invalid character is found, change the background color
, onParseError: function(){
this.css("backgroundColor", "#cc0000")
}
// if the error has been cleared, reset the bgcolor
, onParseClear: function (){
this.css("backgroundColor", "");
}
});
// automatically update the "#minNumber" field every time
// the values are changes via the keyup event
$("input[name^=min]").min("keyup", "#numberMin");
// automatically update the "#minNumber" field every time
// the values are changes via the keyup event
$("input[name^=max]").max("keyup", {
selector: "#numberMax"
, oncalc: function (value, options){
// you can use this to format the value
$(options.selector).val(value);
}
});
// this calculates the sum for some text nodes
$("#idTotalTextSum").click(
function (){
// get the sum of the elements
var sum = $(".textSum").sum();
// update the total
$("#totalTextSum").text("$" + sum.toString());
}
);
// this calculates the average for some text nodes
$("#idTotalTextAvg").click(
function (){
// get the average of the elements
var avg = $(".textAvg").avg();
// update the total
$("#totalTextAvg").text(avg.toString());
}
);
}
);
function recalc(){
$("[id^=total_item]").calc(
// the equation to use for the calculation
"qty * price",
// define the variables used in the equation, these can be a jQuery object
{
qty: $("input[name^=qty_item_]"),
price: $("[id^=price_item_]"),
},
// define the formatting callback, the results of the calculation are passed to this function
function (s){
// return the number as a dollar amount
return "$" + s.toFixed(2);
},
// define the finish callback, this runs after the calculation has been complete
function ($this){
// sum the total of the $("[id^=total_item]") selector
var sum = $this.sum();
$("#grandTotal").text(
// round the results to 2 digits
"$" + sum.toFixed(2)
);
}
);
}
</SCRIPT>
<form name="form1" method="post" action="">
<div id="formContent">
<P id="ex-sum">
<table width="500">
<COL style="width: 50px;">
<COL>
<COL style="width: 60px;">
<COL style="width: 110px;">
<tbody><tr>
<th width="179">
Qty
</th>
<th width="164" align="left">
Product
</th>
<th width="72">
Price
</th>
<th width="65">
Total
</th>
</tr>
<tr>
<td align="center">
<INPUT name="qty_item_1" type="text" class="input" id="qty_item_1" value="1" size="5">
</td>
<td>Table</td>
<td align="center" id="price_item_1">
$150
</td>
<td align="center" id="total_item_1">$</td>
</tr>
<tr>
<td align="center">
<INPUT name="qty_item_2" type="text" class="input" id="qty_item_2" size="5">
</td>
<td>
Pencil</td>
<td align="center" id="price_item_2">
$100</td>
<td align="center" id="total_item_2">$</td>
</tr>
<tr>
<td align="center">
<INPUT name="toys" type="checkbox" id="toys" value="1">
</td>
<td>
Toys</td>
<td align="center" id="price_item_3">
$50</td>
<td align="center" id="total_item_3">$</td>
</tr>
<tr>
<td align="center"><select name="books" id="books">
<option selected="selected">Select an option</option>
<option value="1">Book1</option>
<option value="1">Book2</option>
<option value="1">Book3</option>
</select></td>
<td>
Books</td>
<td align="center" id="price_item_3">
$10</td>
<td align="center" id="total_item_3">$</td>
</tr>
<tr>
<td colspan="3" align="right">
<STRONG>Grand Total:</STRONG>
</td>
<td align="center" id="grandTotal">$</td>
</tr>
</tbody></table>
</div>
</form>
另外,正如您所看到的上面的表单代码,其中使用了表格。无论如何,有没有办法在不使用表格的情况下实现我想要做的事情?
谢谢大家。
解决方案
不是真正的答案。只是一些不适合评论的注释。
我有一种感觉,你只是做了一些复制粘贴,并没有真正理解这个计算插件是如何工作的。现在希望其他人能解决这个问题。
仅举几个例子:
您包含了多个您不使用的函数(最小值、最大值、平均值),并且显示这些值的元素也丢失了。
您的计算代码配置为检查
input
是哪里name
属性开头为qty_item_
但你的checkbox
和select
具有完全不同的名称属性。重新计算主要与
keyup
显然不会触发的事件checkbox
和select
除非用户使用键盘而不是鼠标来选择值你似乎没有使用 jQuery 字段插件 尽管计算插件主页声明如果您想使用与以下不同的输入,则需要它
text
但是因为你让我心情很好,所以我根据你的代码为你制作了一个简单的工作演示页面。
但我不会进一步解释它,还缺少健全性检查(仅允许正整数)和其他内容。
不隶属于 StackOverflow