使用jQuery/Ajax进行数学计算 [英] Mathematical calculations using jQuery/Ajax
本文介绍了使用jQuery/Ajax进行数学计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表格中有这个
<%= f.label :price, "price"%>
<%= f.text_field :price, :size=>20, :id =>"price" %>
<%= f.label :quantity, "Quantity"%>
<%= f.text_field :quantity, :size=>20, :id =>"qty" %>
<%= f.label :amount, "Amount"%>
<%= f.text_field :amount, :size=>20, :id =>"amount"%>
我希望{price * quantity}发生在金额"字段中,只要我在"price"和"quantity"中输入值,即应在提交之前进行计算.我是jQuery/Ajax的新手,所以任何帮助都可以.预先感谢.
I want {price*quantity} to happen inside the 'amount' field as soon as i enter values inside 'price' and 'quantity' i.e calculations should happen before submit. I am new to jQuery/Ajax, so any help will do. Thanks in advance.
推荐答案
您可以执行以下操作
$(function() {
$("#price, #qty").keyup(function() {
var p = $("#price").val();
var q = $("#qty").val();
$("#amount").val(q * p);
});
});
但是您必须在数量字段中指定ID为金额",而不能将价格"作为ID重复使用.
But you have to give the amount field the id "amount" and not reuse "price" as the id.
这篇关于使用jQuery/Ajax进行数学计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文