javascript 使用jQuery自动计算文本框的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26422548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:57:22  来源:igfitidea点击:

Calculate values of textboxes automatically using jQuery

javascriptjquery

提问by kristyan markes

I want to calculate the value of the 1st 2 text boxes without a command button Example i have 3 text boxes.

我想在没有命令按钮的情况下计算第一个 2 个文本框的值示例我有 3 个文本框。

The first 2, where the numbers will be inputed and the last 1 will be the sum or product and so on. Now i want it to auto compute. For example i have inputed values 2 and 3 on the 1st 2 text boxes then automatically the sum or product or whatever result will be displayed on the 3rd text box. How am i able to do this? Thanks

前 2 个,将输入数字,最后一个 1 将是总和或乘积等。现在我希望它自动计算。例如,我在第一个 2 个文本框中输入了值 2 和 3,然后总和或乘积或任何结果将自动显示在第三个文本框中。我怎么能做到这一点?谢谢

    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $('#texttwo').keyup(function(){
    var textone;
    var texttwo;
    textone = parseFloat($('#textone').val());
    texttwo = parseFloat($('#texttwo').val());
    var result = textone + texttwo;
    $('#result').val(result.toFixed(2));
    });
    </script>
    </head>
    <body>
    <input type="text" name="value1" id="textone">
    <input type="text" name="value2" id="texttwo">
    <input type="text" name="result" id="result">
    </body>
    </head>

采纳答案by baao

You can achieve this by using jQuery. Include jQuery in your project by putting this in your <head>

您可以通过使用 jQuery 来实现这一点。通过将其放入您的项目中,将 jQuery 包含在您的项目中<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Then, at the end of the file:

然后,在文件末尾:

<script>
$('#texttwo').keyup(function(){
    var textone;
    var texttwo;
textone = parseFloat($('#textone').val());
texttwo = parseFloat($('#texttwo').val());
var result = textone + texttwo;
$('#result').val(result.toFixed(2));


    });
</script>

This will give you the result when ever you change the value of the second box.

当您更改第二个框的值时,这将为您提供结果。

You will need to do this, too:

你也需要这样做:

Change

改变

    <input type=text name=value1>
    <input type=text name=value2>
    <input type=text name=result>

to:

到:

    <input type="text" name="value1" id="textone">
    <input type="text" name="value2" id="texttwo">
    <input type="text" name="result" id="result">

EDIT

编辑

This is my entire file:

这是我的整个文件:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<input type="text" name="value1" id="textone">
<input type="text" name="value2" id="texttwo">
<input type="text" name="result" id="result">

<script>
    $('#texttwo').keyup(function(){
        var textone;
        var texttwo;
        textone = parseFloat($('#textone').val());
        texttwo = parseFloat($('#texttwo').val());
        var result = textone + texttwo;
        $('#result').val(result.toFixed(2));


    });
</script>