簡體   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