帮助这个代码:)我试着计算价格,我所有的时间提前得到nan结果tnx。
基本上我尝试在codeigniter页面中插入此代码
但我不知道如何摆脱Nan结果
<div class="calculator"> <!--calculator test-->
<div id =a><?php echo $Data->var_price1; ?></div>
<div id =c><?php echo $Data->var_price2; ?></div>
<input id="odrasli" type="number">
<input id="deca" type="number">
<input type="button" value="Get Price" onClick="mycalculator()">
<script>
function mycalculator() {
var a = document.getElementById("a");
var x = document.getElementById("odrasli");
var c = document.getElementById("c");
var y = document.getElementById("deca");
var aa = a * x;
var cc = c * y;
var p = aa + cc;
var d = p * 0.2;
document.getElementById("price").innerHTML = p;
document.getElementById("depozit").innerHTML = d;
}
</script>
</p> Grand total </br><p id="price"> </br>
Deposite<p id="depozit"></p> </br>
</div>
&#13;
答案 0 :(得分:0)
getElementById
返回一个html元素。要获取这些元素的数值,请尝试
var a = parseFloat(document.getElementById("a").innerHTML);
var x = parseFloat(document.getElementById("odrasli").value);
var c = parseFloat(document.getElementById("c").innerHTML);
var y = parseFloat(document.getElementById("deca").value);
答案 1 :(得分:0)
尝试使用此代码段并记住使用parseInt
或parseFloat
function mycalculator() {
var a = parseInt(document.getElementById("a").innerHTML);
var x = parseInt(document.getElementById("odrasli").value);
var c = parseInt(document.getElementById("c").innerHTML);
var y = parseInt(document.getElementById("deca").value);
var aa = a * x;
var cc = c * y;
var p = aa + cc;
var d = p * 0.2;
document.getElementById("price").innerHTML = p;
document.getElementById("depozit").innerHTML = d;
}
&#13;
<div class="calculator">
<div id="a">2</div>
<div id="c">3</div>
<input id="odrasli" type="number">
<input id="deca" type="numbear">
<input type="button" value="Get Price" onClick="mycalculator()">
<div id="price"></div>
<div id="depozit"></div>
&#13;