我如何计算2个输入字段并将结果放在第3个输入字段中 [英] how do i calculate 2 input fields and put results in 3rd input field

查看:64
本文介绍了我如何计算2个输入字段并将结果放在第3个输入字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望具有一种形式,可以在其中将一个值添加到第一个文本字段中,然后将其乘以第二个隐藏字段,然后在第三个文本字段中显示结果.我想要它,因此,只要您在第一个字段中添加一个值,它就会在第三个字段中显示结果,例如在按键上.

i want to have a form whereby i can add a value into the first text field, multiply that by a second hidden field, and display results in a 3rd text field. I want it so as soon as you add a value to the first field it shows the result in the 3rd field, eg on key up.

<label>Qty:</label>
<input name="qty" id="qty" type="text" />
<input name="price" id="price" type="hidden" value="30" /><br />

<label>Total:</label>
<input name="total" id="total" type="text" />

对于jquery来说,任何帮助都是新的.

Any help would be great as im new to jquery.

推荐答案

尽管您得到了答案,但是应该对输入中键入的值进行类型转换. 我的意思是,如果有人键入除整数以外的其他任何值,那么您将在第3个文本框中输入NaN.这是解决此问题的解决方案

Though you got the answer but you should type cast the values you type in inputs. I mean what if someone type any other value other than integer... then you will get NaN in 3rd textbox..Here is solution to handle this problem

$(document).ready(function(){
    var qty=$("#qty");
    qty.keyup(function(){
        var total=isNaN(parseInt(qty.val()* $("#price").val())) ? 0 :(qty.val()* $("#price").val())
        $("#total").val(total);
    });
});

或者您可以检查jsfiddle http://jsfiddle.net/ugPxf/

or you can check jsfiddle http://jsfiddle.net/ugPxf/

这篇关于我如何计算2个输入字段并将结果放在第3个输入字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆