Javascript 当在文本框中键入值时,我需要调用一个函数
当在文本框中键入输入时,我需要调用函数tax,并在另一个文本框中查看结果。我正在使用codeigniter和mysql。 我写了一个函数,比如Javascript 当在文本框中键入值时,我需要调用一个函数,javascript,php,codeigniter,Javascript,Php,Codeigniter,当在文本框中键入输入时,我需要调用函数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_t
<?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"/>
当我在上面的文本框中键入内容并在另一个文本框中显示时,如何调用此函数
有人能帮我吗…我想这里有一个交换功能。
如果文本框的值发生变化,这将调用您的税务功能。此代码对我有效
<input type="number" name="product_price" id="pro_Price"/>
<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>
函数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})$;
如果(!icost.match(重现性)){
警报('请提供0.00格式的项目成本!');
}
否则{
var itotal=(icost)*itax;
itotal*=100;
itotal=Math.ceil(itotal);
itotal/=100;
如果(itotal.toFixed){
itotal=itotal.toFixed(2);
}
document.getElementById('mytotal')。value=itotal;
}
}
函数getSalesTax(){
var taxarray=document.myform.mytax;
//var retax='^[1]{1}\[0-9]{1,4}$';
var i;
对于(i=0;i
您可以将php代码转换为javascript并将其放在页面中,也可以进行Ajax调用以进行计算并返回结果。感谢您的支持我将使用onChange()
<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()"/> VAT
<br />
<input name="mytax" type="radio" value="0.1725" onClick="getOrderTotal()"/> CST
<br />
<input name="mytax" type="radio" value="1.00" onClick="getOrderTotal()" checked="checked"/> Other (no sales tax)
</br>
<!--<span class="help-inline">Cost Price</span>-->
</div>
</div>