如何在onkepress事件+ JS上计算sub_total和total_price输入
How to calculate sub_total and total_price inputs on onkepress event + JS
如何将Qty
文本框与Price
文本框相乘,然后在Subtotal
文本框上打印输出?然后将Subtotal
文本框的总和相加,并在Total Price
文本框上用按键事件打印总和。
<ul>
<li>
Qty<input type="text" name="item_qty0" id="item_qty0" class="item_qty valid" onkeypress="return calProduct(this);" value="1" autocomplete="off" />
Price<input type="text" name="item_price0" id="item_price0" class="item_price valid" onkeypress="return calProduct(this);" value="0" autocomplete="off" />
Subtotal<input type="text" name="item_subtotal0" id="item_subtotal0" class="item_subtotal" disabled="" />
</li>
<li>
Qty<input type="text" name="item_qty1" id="item_qty1" class="item_qty valid" onkeypress="return calProduct(this);" value="1" autocomplete="off" />
Price<input type="text" name="item_price1" id="item_price1" class="item_price valid" onkeypress="return calProduct(this);" value="0" autocomplete="off" />
Subtotal<input type="text" name="item_subtotal1" id="item_subtotal1" class="item_subtotal" disabled="" />
</li>
</ul>
<hr />
<p style="text-align:right;">
Total Price<input name="total_price" maxlength="15" type="text" id="totalPrice" />
</p>
演示Here is The working DEMO http://jsfiddle.net/WLSND/
$("input").keyup(function(){
var total = 0;
$('.item_subtotal').each(function (index, element) {
var subtotal = parseInt($(this).parent().find(".item_qty").val())*parseInt($(this).parent().find(".item_price").val()) ;
$(this).val(subtotal);
total = total + subtotal;
});
$("#totalPrice").val(total);
$('input').keyup(function(){
var v = this.value, el = $(this);
if(!isNaN(v)){
var ov = el.siblings('.valid').val();
el.siblings().last().val(v*ov);
$(this).removeClass('nope').trigger('totalChange');
} else {
$(this).addClass('nope');
}
});
$(document).on('totalChange', function(){
var sub1 = parseFloat($('#item_subtotal').val(), 10);
var sub2 = parseFloat($('#item_subtotal2').val(), 10);
$('#totalPrice').val(sub1+sub2);
});
小提琴
我也做了一些改变在HTML
关于ID
s
相关文章:
- JavaScript Pub/Sub属性访问问题
- Javascript运行Total保持添加
- babel with sub js files
- 使用running total时使用Javascript$NaN
- three.js:three.Vector3.sub()动作怪异
- 在添加的输入字段中计算TOTAL
- 使用中介pub/sub模式时路由骨干应用程序
- Cross Sub Domain Cookie
- 如何使此pub/sub代码可读性更强
- underscore.js groupBy sub-array
- Addy Osmanis pub/sub,难以理解代码
- Openfire 会在一天后删除 pub sub 节点(而不是在服务器重新启动或删除缓存后)
- javascript document.queryCommandState() “superscript” / “sub
- 未捕获的类型错误:无法读取 Google Analytics API 中 null 的属性“sub”
- XMLHttpRequest onprogress total在Chrome上始终为0
- redis sub/pub in or out of io.connect callback
- RequireJS, Pub/Sub.如果没有糟糕的解决方法,我无法从其自己的方法访问视图实例
- Javascript: ForEach over sub objects
- Opencart - jQuery cart total update - 修改输出
- 使用 JQuery 将表单解析为 JSON 对象,并考虑 sub – JSON 对象