douzhong1730 2014-06-04 05:01
浏览 30
已采纳

根据使用javascript或PHP的输入自动减去没有按钮点击的数字

I have more than 2 inputs that can only accept numbers and there is a displayed number on the top. How could I subtract the numbers that I have entered just by changing the value on any of the input boxes? Please Help.

 <p id='Answer'> 100 </p>
 <input type='number' max='10' min='0' name='num1'>
 <input type='number' max='5' min='0' name='num2'>
  • 写回答

3条回答 默认 最新

  • dongmeiyi2266 2014-06-04 05:08
    关注

    Ok your question is a little ambiguous. Anyway I have assumed you want the following. You want the num1 to subtract automatically from num2 and be displayed in id='Answer'.

    Following is the code to that in jQuery.

    $(".sub").focusout(function() {
      $("#answer").html('');
      var num1 = $("#num1").val();
      var num2 = $("#num2").val();
      var answer = num1 - num2;
      $("#answer").html(answer);
    });
    

    You can view the demo here

    In case you want both input fields to subtract from 100. Following is the code.

    $(".sub").focusout(function() {
    $("#answer").html('');
    var num1 = $("#num1").val();
    var num2 = $("#num2").val();
    var answer = 100 - num1 - num2;
    $("#answer").html(answer);
    });
    

    And following is the link to demo

    Based on your question in last comment. Download jquery from here. Now include the downloaded script in your header. Or just use the following code.

    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    

    After you have included jquery you need to use your .focusout code like stated below

    <script type="text/javascript">
    $(document).ready(function() {
       $(".sub").focusout(function() {
         $("#answer").html('');
         var num1 = $("#num1").val();
         var num2 = $("#num2").val();
         var answer = 100 - num1 - num2;
         $("#answer").html(answer);
        });
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?