且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何计算2个输入字段并将结果放在第3个输入字段中

更新时间:2023-12-01 10:44:04

尽管您得到了答案,但是应该对输入中键入的值进行类型转换. 我的意思是,如果有人键入除整数以外的其他任何值,那么您将在第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/