我需要在文本框中键入值时调用函数(I need to call a function when value is typed in textbox)

编程入门 行业动态 更新时间:2024-10-28 12:30:21
我需要在文本框中键入值时调用函数(I need to call a function when value is typed in textbox)

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

当我在上面的文本框中键入内容并将其显示在另一个文本框中时,如何调用此函数有人可以帮助我...

I need to call a function tax when input is typed in a text box and view the result in another textbox. I am using codeigniter and mysql. I wrote a function tax like

<?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; } ?>

And i have a textbox like

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

how do i call this function when i type something in the above text box and display them in another textbox Can someone help me...

最满意答案

这段代码适合我

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

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

This code works for me

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

Above is my script I used text fields and displayed the results

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

Thank you all for your support.... :)

更多推荐

本文发布于:2023-07-21 09:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   框中   函数   文本   typed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!