如何计算两个输入表单字段并将值放入另一个而不使用jquery提交表单? [英] How to calculate two input form fields and put the value in another without submitting the form using jquery?

查看:85
本文介绍了如何计算两个输入表单字段并将值放入另一个而不使用jquery提交表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常基本的问题,但我坚持这一点。
我有两个输入框,我想要计算这些输入并立即将结果显示到另一个输入(第三个),而无需提交按钮。
我的意思是,一旦我开始在输入中输入值,结果将显示在第3个输入中的实时表示结果...

I know this is an extremely basic question but I'm stuck on this. I have two input boxes and I want to calculate those inputs and show the result into another input (3rd one) immediately without the need of a submit button. I means, once I start entering the values into the input and the result will shows live in the 3rd input which is the represent the result...

<input type="text" class="input value1">
<input type="text" class="input value2">
<input type="text" disabled="disabled" id="result">

<script>
 $(document).ready(function(){
      var val1 = $(".value1").val();
      var val2 = $(".value2").val();
      $(".input").keyup(function(){
         $("#result").text(val1+val2);
       });
});
</script>


推荐答案

将val1和val2放在keyup语句中。

Put val1 and val2 in keyup statement.

$(document).ready(function(){
    $(".input").keyup(function(){
          var val1 = +$(".value1").val();
          var val2 = +$(".value2").val();
          $("#result").val(val1+val2);
    });
});

看看小提琴: http://jsfiddle.net/g7zz6/

这篇关于如何计算两个输入表单字段并将值放入另一个而不使用jquery提交表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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