繁体   English   中英

使用Javascript添加两个输入框

[英]Adding two input boxes using Javascript

我正在尝试添加两个已经计算和格式化的输入字段,但运气不佳。 我的代码是:

<input type='text' id='cage_linear_feet' value='83'>
<input type='text' id='cage_estimate' disabled>
<br>
<input type='text' id='cage_doors' value='3'>
<input type='text' id='doors_estimate' disabled>
<br>
<input type='text' id='cage_totals' disabled>
<script>
   function format(n) {
       return n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
   }

   //Linear Feet Calculation

      $(document).ready(LinearFeet); 
      document.getElementById('cage_linear_feet').addEventListener("keyup", LinearFeet);

      var inputBox1 = document.getElementById('cage_linear_feet');

      function LinearFeet(){
         document.getElementById('cage_estimate').value = format(inputBox1.value*225);
      }

   //Doors Calculation

      $(document).ready(CageDoors); 
      document.getElementById('cage_doors').addEventListener("keyup", CageDoors);

      var inputBox2 = document.getElementById('cage_doors');

      function CageDoors(){
         document.getElementById('doors_estimate').value = format(inputBox2.value*1800);
      }

</script>

如何将cage_estimatedoors_estimate一起添加并实时显示在cage_totals中?

谢谢,

约翰

这就是你要问的

如何在Java脚本中将逗号分隔的货币转换为数字

  parseFloat 

此函数计算总计。 您必须在每个关键功能中调用它。

  function setTotal(){
      var x=document.getElementById('doors_estimate').value;
      var y=document.getElementById('cage_estimate').value;

      if(x ){
        if(y){
            var z=(parseFloat(x.replace(',',''))+parseFloat(y.replace(',','')));            
            document.getElementById('cage_totals').value=format(z);
        }
      }
  }

呼叫代码

  function LinearFeet(){
    var inputBox1 = document.getElementById('cage_linear_feet');        
     document.getElementById('cage_estimate').value = format(inputBox1.value*225);
     setTotal();
  }


  function CageDoors(){
    var inputBox2 = document.getElementById('cage_doors');      
     document.getElementById('doors_estimate').value = format(inputBox2.value*1800);
     setTotal();
  }

参考资料:

parseFloat

更换

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM