javascript - 从 jQuery 中的数量输入框计算价格

标签 javascript jquery html input

我在根据 jQuery 中的数量计算价格时遇到了一个小问题。这些是我的输入框。

Quantity: <input type="text" style="width: 50px;" name="quantity" id="quantity" class="txt" value="1" />

Price: <input type="text" style="width: 50px;" name="item_price" id="item_price" class="txt" value="2990" />

Total Price: <input type="text" style="width: 50px;" name="total_price" id="total_price" class="txt" value="" />

最佳答案

我假设您需要公式或如何获取输入元素的值:

var quantity = $("#quantity").val();
var iPrice = $("#item_price").val();

var total = quantity * iPrice;

$("#total_price").val(total); // sets the total price input to the quantity * price

alert(total);

为 Keyup 编辑:

$('#quantity').keyup(function() {
   var quantity = $("#quantity").val();
   var iPrice = $("#item_price").val();

   var total = quantity * iPrice;

   $("#total_price").val(total); // sets the total price input to the quantity * price
});

关于javascript - 从 jQuery 中的数量输入框计算价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12728092/

相关文章:

javascript - Uncaught ReferenceError : variable is not defined on onclick function Javascript

java - Javascript 构造函数与 Java 中的类或接口(interface)等效/相似吗

javascript - 将数组中的所有对象合并为一个

javascript - D3.js 散点图与套索 - 记录选择

javascript - 选项卡仅在加载时显示过多内容,单击后自行修复

html - 底部和右侧绝对定位不适用于 IE

css - 在wordpress中添加自定义菜单

javascript - 使用 jquery 或 javascript 更改表格单元格的高度属性值

javascript - jQuery 输入[名称 =""] 选择器不起作用

javascript - 使用jquery延迟图像加载