2015-06-25 106 views
3

當在文本框中鍵入輸入並在另一個文本框中查看結果時,需要調用函數稅。我正在使用codeigniter和mysql。 我寫了一個函數稅務像我需要在文本框中鍵入值時調用函數

<?php 
function tax($amount_without_tax, $tax){ 
    $amount_with_tax = $amount_without_tax + ($tax*($amount_without_tax/100)); 
    // work out the amount of vat 
    $amount_with_tax = round($amount_with_tax, 2); 
    return $amount_with_tax; 
} 
?> 

,我已經喜歡

<input type="number" name="product_price"/> 

我如何調用該函數時,我在上面的文本框中鍵入的東西,在另一個文本 可以顯示他們的文本框有人幫我...

+0

您可以將php代碼轉換爲javascript並將其放在您的頁面中,或者您可以進行Ajax調用以執行計算並返回結果。 – jeff

回答

1

此代碼對我的作品

<script type="text/javascript"> 
      function getOrderTotal() { 
       var icost = document.myform.myprice.value; 

       var itax = getSalesTax(); 

       var recurrency = '^[0-9]{1,5}\.[0-9]{2}$'; 
       var reitems = '^([1-9])([0-9]{0,2})$'; 

       if(!icost.match(recurrency)) { 
        alert('Please provide an item cost in 0.00 format!'); 
       } 
       else { 
        var itotal = (icost) * itax; 
        itotal *= 100; 
        itotal = Math.ceil(itotal); 
        itotal /= 100; 
        if(itotal.toFixed) { 
         itotal = itotal.toFixed(2); 
        } 

        document.getElementById('mytotal').value = itotal; 
       } 
      } 

      function getSalesTax() { 
       var taxarray = document.myform.mytax; 
       //var retax = '^[1]{1}\.[0-9]{1,4}$'; 
       var i; 

       for(i=0; i<taxarray.length; i++) { 
        if(taxarray[i].checked) { 
         if(!taxarray[i].value) { 
          alert('Please provide a tax rate from the button list only!'); 
          return 0; 
         } 
         else { 
          return parseFloat(taxarray[i].value); 
         } 
        } 
       } 

       return 1.0; 
      } 
     </script> 

以上是我的腳本 我使用的文本字段和顯示結果

<div class="control-group"> 
      <label for="inputError" class="control-label">Wholesale Price</label> 
      <div class="controls"> 
      <input name="myprice" type="text" id="myprice" size="10" maxlength="10" onChange="getOrderTotal()" value="0.00" /> 

      </div> 
      </div> 
      <div class="control-group"> 

      <label for="inputError" class="control-label">Total amount</label> 
      <div class="controls"> 
       <input type="text" id="mytotal" name="mytotal" value="0.00"> 
       <!--<span class="help-inline">Cost Price</span>--> 
      </div> 
<div class="control-group"> 
      <label for="inputError" class="control-label">Tax Details</label> 
         <input name="mytax" type="radio" value="0.145" onClick="getOrderTotal()"/>&nbsp;VAT 
         <br /> 
         <input name="mytax" type="radio" value="0.1725" onClick="getOrderTotal()"/>&nbsp;CST 
         <br /> 
         <input name="mytax" type="radio" value="1.00" onClick="getOrderTotal()" checked="checked"/>&nbsp;Other (no sales tax) 
         </br> 
       <!--<span class="help-inline">Cost Price</span>--> 
      </div> 
      </div> 

謝謝一切爲了你的支持t .... :)

0

我覺得有一個onChange功能。
<input type="number" name="product_price" onChange = "tax(x,y);"/>
如果文本框的值發生變化,這個schould會調用您的稅收功能。

+0

感謝您的支持,我將與onChange() – Anu

0
<input type="number" name="product_price" id="pro_Price"/> 

的Jquery:

$("#pro_Price").change(function() { 
    alert("Handler for .change() called."); 
    //Do your calculation here with this.value 

}); 
相關問題