【发布时间】:2013-05-25 03:24:43
【问题描述】:
来自瑞典的下午好! 我的 javascript 代码有问题,它在试图让它从我的成绩中计算我的分数时给了我“NaN”。它应该检查它是哪个分数,然后取它的价值并将其添加到最终分数中,当按下按钮时会发出警报。 Javascript:
var biobetyg;
var bildbetyg;
function bBio() {
var bio = document.getElementById("bio");
var biobe = bio.options[bio.selectedIndex].value;
if ((biobe === "-") || (biobe === "f")) {
biobetyg = 0;
} else if (biobe === "e") {
biobetyg = 10;
} else if (biobe === "d") {
biobetyg = 12.5;
} else if (biobe === "c") {
biobetyg = 15;
} else if (biobe === "b") {
biobetyg = 17.5;
} else if (biobe === "a") {
biobetyg = 20;
}
}
function bBild() {
var bild = document.getElementById("bild");
var bildbe = bild.options[bild.selectedIndex].value;
if ((bildbe === "-") || (bildbe === "f")) {
bildbetyg = 0;
} else if (bildbe === "e") {
bildbetyg = 10;
} else if (bildbe === "d") {
bildbetyg = 12.5;
} else if (bildbe === "c") {
bildbetyg = 15;
} else if (bildbe === "b") {
bildbetyg = 17.5;
} else if (bildbe === "a") {
bildbetyg = 20;
}
}
var merit = bildbetyg + biobetyg;
function GetSelectedItem() {
var strSel = "The Value is: " + merit + " and text is: ";
alert(strSel);
}
HTML:
<form>
<h1>Bild</h1>
<h2>Välj ditt betyg i bild:</h2>
<select id="bild" onchange="bBild()">
<option class="f" value="-">-</option>
<option class="f" value="f">F</option>
<option class="go" value="e" selected>E</option>
<option class="go" value="d">D</option>
<option class="go" value="c">C</option>
<option class="go" value="b">B</option>
<option class="go" value="a">A</option>
</select>
<select id="bio" onchange="bBio()">
<option class="f" value="-">-</option>
<option class="f" value="f">F</option>
<option class="go" value="e" selected>E</option>
<option class="go" value="d">D</option>
<option class="go" value="c">C</option>
<option class="go" value="b">B</option>
<option class="go" value="a">A</option>
</select>
<input type="button" value="Press to Confirm" onClick="GetSelectedItem();">
</form>
【问题讨论】:
-
旁白:Google 'javascript switch'
-
为什么不将选项的值设为这些数字?
标签: javascript forms select if-statement options