使用jquery onkeypress进行计算
Calculation using jquery onkeypress
我在jquery中有这种类型的练习。转到这里http://jsfiddle.net/LAntL/3/
对于Item 1
,如果我添加Size 7
数量的#no,则文本框中应显示multiplied by Item 1's Price
,结果应显示在Item 1's Ext
文本框中。那么如果我对size 8.5 of Item 1
做同样的操作,它应该执行同样的操作。
但是Ext将有(Price x Size 7 Qty) + (Price x Size 8.5 Qty)
Item 2
也是如此。同时,Total quantity and Total price
将在每个文本框的keypress
事件上更新。
我是jquery的初学者,做了这个致命的练习。请帮帮别人。
提前谢谢。
我建议您为字段分配一些类,以便jQuery可以轻松地找到它们:
"lineQty" - add this class to all the qty fields
"lineTotal" - add this class to all the line total fields
"linePrice" - I think you can see where I'm going with this
"lineExt" - add to all ext fields
然后,以下.keyup
函数将自动更新所有行。
$(document).ready(function() {
$(".lineQty").keyup(function() {
// 'this' is the field the user is typing in
// so find the tr that it belongs to
var $row = $(this).closest("tr"),
total = 0,
// get the price for the current row:
price = $row.find(".linePrice").val();
// loop through every qty field for the current row and add
// the entered value to the total, using unary plus to convert
// to a number, and a default of 0 if non-numeric data is entered
$row.find(".lineQty").each(function() {
total += +this.value || 0;
});
// set the total for the current row
$row.find(".lineTotal").val(total);
// set the price for the current row
$row.find(".lineExt").val(total * price);
// now add up the grand totals
var grandTotal = 0,
grandPrice = 0;
$(".lineTotal").each(function() {
grandTotal += +this.value || 0;
});
$(".lineExt").each(function() {
grandPrice += +this.value || 0;
});
$("[name='data[totalQuantity]']").val(grandTotal);
$("[name='data[totalPrice]']").val(grandPrice);
});
});
工作演示:http://jsfiddle.net/LAntL/5/(只适用于第一行,因为我不愿意为所有字段分配类-你需要按照我在第一行中所做的将我上面谈到的类添加到每个字段中)。
好吧,你想学习一些东西,所以我将描述一种可能的方法,而不给你最终的解决方案。
首先,您应该用class="item"
标记包含项目的表。现在,您可以在jQuery中编写一个选择器,它可以分别获取每个项。然后,您应该标记所有具有特殊功能的单元格(<td>
)。总计、价格、光盘和分机。您的表格应该是这样的:
<table class="item">
<tr>
<td>...</td>
<td class="total">...</td>
<td class="price">...</td>
<td class="disc">...</td>
<td class="ext">...</td>
</tr>
</table>
现在,您可以使用类"item"为表中的所有输入字段编写一个选择器。
$('.item input').change(function(){});
在该函数内部,您可以处理计算。一开始,我建议获得项目:
$('.item input').change(function(){
var item = $(this).closest('table.item');
var totalInput = $(item).find('.total input');
var priceInput = $(item).price('.price input');
// ...
var allWriteableInputs = $(item).find('input[readonly!="readonly"]');
// do the calculation
var sum = 0;
$(allWriteableInputs).each(function(i, input){
sum += $(input).val();
});
$(totalInput).val(sum);
});
如果你走这条路,你不需要:
onkeypress="return isNumberKey(event);"
和- 每个输入框的
id="product1Total"
我希望这种发人深省的冲动能对你有所帮助。你还有很长的路要走,我年轻的学徒;-)
- 根据元素和容器大小计算边距
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 使用D3.js计算带有字母间距的文本长度
- 使用CSS或JavaScript计算分页符的数量
- 可以't计算自定义谷歌地图的js
- 如何计算每个元素's的高度,并将这些值用作函数中的变量
- JavaScript计算帮助(乘以时间)
- 如何计算对象文字中的键
- JavaScript循环无法正确计算/显示结果
- 与域在同一台计算机上运行的NODEJS服务器的CORS错误
- 四舍五入JavaScript计算
- 计算HTML中的页数
- 使用jQuery计算数组中的对象以更改进度条
- 如何在jquery中使用实时计算求和值
- 计算多个项目的价格
- 计算CSS3缩放框在另一个框中的最高位置
- 如何计算二十面体的法线
- 使用jquery onkeypress进行计算
- 做一些计算在javascript onkeypress
- Javascript不能正确地计算onKeyPress元素的长度