2014-09-30 89 views
0

我正在爲遊戲創建一個計算器,我想使用下拉菜單在switch語句中選擇一個值;雖然它似乎沒有工作,但它總是返回0.任何人都可以看到我出錯的地方嗎?另外我對java和html相當新,所以很抱歉如果有一個愚蠢的錯誤。使用<select>標籤,使用Switch語句(Java和HTML)

http://jsfiddle.net/1r177khr/

這裏是我的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

控制檯說calcDarkTroops();沒有定義。 – 2014-09-30 04:28:52

+0

您的Java看起來像是javascript – RahulB 2014-09-30 04:29:43

+0

'String maxString' - >'var maxString' – 2014-09-30 04:43:31

回答

0

嘗試提供的價值屬性的選擇選項,並且還考慮提供默認選項,

<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> 

FIDDLE

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