简体   繁体   English

产品价格计算器不起作用

[英]Product price calculator isn't working

I'm creating an order form in which users can select an amount of products and it should immediately change total prices. 我正在创建一个订单表单,用户可以在其中选择一定数量的产品,并且应立即更改总价格。 I made some HTML markup and jquery code to achieve this, but I can't get it working. 我做了一些HTML标记和jquery代码来实现这一点,但我无法让它工作。 Can anyone see what I'm doing wrong? 谁能看到我做错了什么?

Fixed script: 修正脚本:

$("input[type=number]").change(function () {
    var value = parseFloat($(this).attr("title"));
    var total = (value * this.value).toFixed(2);
    if (total < 0) total = 0;
    $(this).parent().next().find("input").val('€' + total);
});

Inside of event handler this refers to HTMLInputElement, not jQuery instance so you have to wrap it in $(this) . 在事件处理程序内部, this指的是HTMLInputElement,而不是jQuery实例,所以你必须将它包装在$(this)

Also you need to traverse one level up to the parent node and then use next() to get target input field. 您还需要遍历一个级别到父节点,然后使用next()来获取目标输入字段。

Finally I added basic validation to prevent negative prices. 最后,我添加了基本验证以防止负价格。 You can also add min="0" attribute to input fields. 您还可以将min="0"属性添加到输入字段。

Demo: http://jsfiddle.net/dfsq/X33pS/ 演示: http//jsfiddle.net/dfsq/X33pS/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM