如何在表单中计算数学,并使用javascript将数据插入文本输入?

如何在表单中计算数学,并使用javascript将数据插入文本输入?,javascript,html,Javascript,Html,我一直在寻找一个解决这个问题的办法,大约3天都没有用。所以我决定发布一个关于它的问题 在这个表单中,我试图从两个表单元素中获取数据,计算出百分比,然后将数据输入到文本输入和隐藏输入元素中 HTML getOrderTotal未在脚本中定义 var服务费={}; 服务费[“无”]=0; 服务费[“参考价值减去”]=7.5; 服务费[“参考价格”]=12.5; 服务费[“代表费减”]=10; 服务费[“代表你更多”]=15; 服务费[“dbl_reprep_you_less”]=17.5; 服务费

我一直在寻找一个解决这个问题的办法,大约3天都没有用。所以我决定发布一个关于它的问题

在这个表单中,我试图从两个表单元素中获取数据,计算出百分比,然后将数据输入到文本输入和隐藏输入元素中

HTML


getOrderTotal
未在
脚本中定义

var服务费={};
服务费[“无”]=0;
服务费[“参考价值减去”]=7.5;
服务费[“参考价格”]=12.5;
服务费[“代表费减”]=10;
服务费[“代表你更多”]=15;
服务费[“dbl_reprep_you_less”]=17.5;
服务费[“dbl_reprep_you_more”]=27.5;
服务费[“dbl_repref_you_less”]=15;
服务费[“dbl_repref_you_more”]=25;
函数getServicePrice(){
var ServicePrice=0;
var theForm=document.forms[“req_serv”];
var selectedservtype=form.elements[“serffee”];
服务价格=服务费[selectedservtype.value];
退货价格;
}
函数getOrderTotal(){
return document.getElementById('ordertot').value;
}
函数计算器总计(){
var reqserffee=(getServicePrice()/100)*getOrderTotal();
var txtobj=document.getElementById('totalPrice');
txtobj.style.display='block';
txtobj.value=reqservfee;
}
函数hideTotal(){
var txtobj=document.getElementById('totalPrice');
txtobj.style.display='none';
}

订单总数:
服务及费用
选择服务
退款
退款++
替代品
替代品++
二次探底
二次探底++
二次探底+++
二次探底++++

您支付的费用:
您得到的结果或错误是什么?
getOrderTotal
未定义。。。我在你的代码中没有看到它…没有错误,只是没有显示更改谢谢,我没有看到我忘记添加了that@c0d3x1337-我很高兴这有帮助!很高兴编一个问题,但你能看出来我的计算是否正确吗?试图获取th金额的百分比,但看起来不正确。基本上它设置了12345.18=0.11的15.5%,这似乎不对啊,我明白了。我忘了封装它。我想我不会做数学来救我的命,哈哈。
<head><script type="text/javascript" src="vali.js" async></script></head>
<body onload="hideTotal()">
<form class="form-horizontal" id="req_serv" name="req_serv" method="post" action="index.php">
<label>Order Total :</label>
<input class="form-control" id="ordertot" type="number" name="ordertot" placeholder="$0.00" onkeyup="calculateTotal()" />
<span class="error"><?php echo $ordertotError;?></span>
<label>Service & Fee</label>
<select class="form-control" id="servfee" name="servfee" onchange="calculateTotal()">
    <option value="none">Select Service</option>
    <option value="ref_thou_less">Refunds</option>
    <option value="ref_thou_more">Refunds++</option>
    <option value="rep_thou_less">Replacements</option>
    <option value="rep_thou_more">Replacements++</option>
    <option value="dbl_reprep_thou_less">Double Dip</option>
    <option value="dbl_reprep_thou_more">Double Dip++</option>
    <option value="dbl_repref_thou_less">Double Dip+++</option>
    <option value="dbl_repref_thou_more">Double Dip++++</option>
</select>
<br/>
<label>What You Pay: </label>
<input type="number" required="required" id="totalPrice" name="tp">
<input class="submit" type="submit" name="submit" value="Submit">
<span class="success"><?php echo $successMessage;?></span>
</form>
</body>
 var service_fee=new Array();
 service_fee["none"]=0;
 service_fee["ref_thou_less"]=7.5;
 service_fee["ref_thou_more"]=12.5;
 service_fee["rep_thou_less"]=10;
 service_fee["rep_thou_more"]=15;
 service_fee["dbl_reprep_thou_less"]=17.5;
 service_fee["dbl_reprep_thou_more"]=27.5;
 service_fee["dbl_repref_thou_less"]=15;
 service_fee["dbl_repref_thou_more"]=25;

//This function finds the service type price based on the 
//drop down selection
function getServicePrice()
{
    var ServicePrice=0;
    //Get a reference to the form id="req_serv"
    var theForm = document.forms["req_serv"];
    //Get a reference to the select id="servfee"
    var selectedservtype = theForm.elements["servfee"];

    ServicePrice = service_fee[selectedservtype.value];

    //finally we return service price
    return ServicePrice;
}

function calculateTotal()
{
    //Here we get the total price by calling our function
    //Each function returns a number so by calling them we add the values they return together
    var reqservfee = getServicePrice() * 100 / getOrderTotal();

    //display the result
    var txtobj = document.getElementById('totalPrice');
    txtobj.style.display='block';
    txtobj.value = reqservfee;
}

function hideTotal()
{
    var txtobj = document.getElementById('totalPrice');
    txtobj.style.display='none';
}