【问题标题】:I need to call a function when value is typed in textbox在文本框中输入值时我需要调用一个函数
【发布时间】:2015-09-11 13:48:21
【问题描述】:

当在文本框中输入输入时,我需要调用函数 tax 并在另一个文本框中查看结果。我正在使用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"/>

当我在上面的文本框中键入内容并将它们显示在另一个文本框中时,如何调用此函数 谁能帮帮我...

【问题讨论】:

  • 您可以将 php 代码转换为 javascript 并将其放置在您的页面中,也可以进行 Ajax 调用来进行计算并返回结果。

标签: javascript php codeigniter


【解决方案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>

感谢大家的支持.... :)

【讨论】:

    【解决方案2】:

    我认为有一个 onChange 功能。
    &lt;input type="number" name="product_price" onChange = "tax(x,y);"/&gt;
    如果文本框的值发生变化,这应该调用您的税收功能。

    【讨论】:

    • 感谢您的支持,我将使用 onChange()
    【解决方案3】:
    <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
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多