我有一个HTML表单和一个Javascript,但是这个Javascript执行的计算不正确
function hitung(){
var e = (document.getElementById("ifin").value);
var panjang = parseFloat (document.getElementById("ipanjang").value);
var tinggi = parseFloat (document.getElementById("itinggi").value);
var ht = 2.0;
var hf = 0.0;
var total = 0.0;
if (e == "hpl") {
hf = 0.0;
}
else if (e == "cat") {
hf = 0.5;
}
else {
hf = 0.0;
}
total = panjang*tinggi*ht+hf;
document.getElementById("ototal").innerHTML =
"Harga Total : Rp." + total.toFixed(2) + "0.000,00" + "<br>Sudah termasuk ongkir";
<select name="ifin" id="ifin" class="form-control input-lg" required >
<option selected disabled >Select Finishing</option>
<option value="hpl">HPL</option>
<option value="cat">Cat Duco</option>
</select>
<br>
<h3 align="center"> Masukkan Ukuran </h3>
<div class="form-group row">
<div class="col-xs-6" align="center">
<input class="form-control" type="number" placeholder="Panjang" min="1" id="ipanjang" >
</div>
<div class="col-xs-6">
<input class="form-control" type="number" placeholder="Tinggi" min="1" id="itinggi">
</div>
</div>
结果不是我想要的,hf
不计算,只计算panjang*tinggi*hf
答案 0 :(得分:0)
我相信你的错 var e = document.getElementById(“ifin”)。value; 尝试排除括号。
答案 1 :(得分:0)
您必须先选择select
代码,然后提取有效选项
var e = document.getElementById("ifin")
var rm = e.options[e.selectedIndex].value
if(rm == "hpl") {//do stuff}
答案 2 :(得分:0)
function hitung(){
var e = (document.getElementById("ifin").value);
var panjang = parseFloat (document.getElementById("ipanjang").value);
var tinggi = parseFloat (document.getElementById("itinggi").value);
var ht = 2.0;
var hf = 0.0;
var total = 0.0;
if (e == "hpl") {
//hf = 0.0;
hf = 1.0;//I wanted 3rd value to make sure it works
}
else if (e == "cat") {
hf = 0.5;
}
else {
hf = 0.0;
}
document.getElementById("hf").innerHTML = "HF = " + hf;
total = panjang*tinggi*ht+hf;
document.getElementById("ototal").innerHTML =
"Harga Total : Rp." + total.toFixed(2) + "0.000,00" + "<br>Sudah termasuk ongkir";
}
<select name="ifin" id="ifin" class="form-control input-lg" required >
<option selected disabled >Select Finishing</option>
<option value="hpl">HPL</option>
<option value="cat">Cat Duco</option>
</select>
<br>
<h3 align="center"> Masukkan Ukuran </h3>
<div class="form-group row">
<div class="col-xs-6" align="center">
<input class="form-control" type="number" placeholder="Panjang" min="1" id="ipanjang" >
</div>
<div class="col-xs-6">
<input class="form-control" type="number" placeholder="Tinggi" min="1" id="itinggi">
</div>
</div>
<button onclick="hitung()">Calculate</button>
<h2 id="ototal"></h2>
<h2 id="hf"></h2>
您的代码对我来说很合适,我添加了一个按钮计算,它将输出值与hf
,运行代码段并选择值并按下计算,您将看到该值。
答案 3 :(得分:-1)