我正在为游戏创建一个计算器,我想使用下拉菜单在switch语句中选择一个值;虽然它似乎没有工作,它总是返回0.谁能看到我哪里出错了?另外我对java和html相当陌生,如果有一个愚蠢的错误,那就很抱歉。
这是我的HTML:
<form onsubmit="return calcDarkTroops(0);" style="display: inline;">
<!-- Title of Form /--> <font>Level</font>
<!-- Gets Input /-->
<select id="minionlevel_input">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
<!-- Calls 'calcDarkTroops' /-->
<input type="button" value="Calculate" onclick="calcDarkTroops();">
<!-- Outputs result /--> <span id="minioncost_result"> = 0 Dark Elixer</span>
这是我的Java:
calcDarkTroops = function () {
var minionlevel = Number(document.getElementById("minionlevel_input").value);
String maxString;
switch (minionlevel) {
case 1:
maxString = "200,000";
break;
case 2:
maxString = "190,000";
break;
case 3:
maxString = "170,000";
break;
case 4:
maxString = "140,000";
break;
case 5:
maxString = "100,000";
break;
case 6:
maxString = "0";
document.getElementById("minioncost_result").innerHTML = "= " + Math.round(maxString * 100) / 100 + " Dark Elixer";
}
答案 0 :(得分:0)
尝试为您的选择选项提供value属性,并考虑提供默认选项
<select id="minionlevel_input">
<option value="0">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
答案 1 :(得分:0)
试试这个:
<form style="display: inline;">
<!-- Title of Form /--> <font>Level</font>
<!-- Gets Input /-->
<select id="minionlevel_input">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
<!-- Calls 'calcDarkTroops' /-->
<input type="button" value="Calculate" onclick="return calcDarkTroops(document.getElementById('minionlevel_input').value)">
<!-- Outputs result /--> <span id="minioncost_result"> = 0 Dark Elixer</span>
<script>
function calcDarkTroops(minionlevel) {
var maxString;
switch (minionlevel) {
case '1':
maxString = "200,000";
break;
case '2':
maxString = "190,000";
break;
case '3':
maxString = "170,000";
break;
case '4':
maxString = "140,000";
break;
case '5':
maxString = "100,000";
break;
case '6':
maxString = "0";
break;
}
document.getElementById("minioncost_result").innerHTML = "= " + Math.round(parseInt(maxString) * 100) / 100 + " Dark Elixer";
}
请参阅此fiddle