2015-12-06 39 views
1

我在下面的代碼中遇到問題。我試圖計算出飲酒後允許有人駕駛的時間。公式爲:酒精量* 1.25(男性)或1.5(女性)+最後一杯酒精消耗時間= driveTime。在html和Java中使用表單和函數時錯誤的計算

問題是該公式使用「1」而不是「1.25」或「1.5」進行計算。

有人知道我在做什麼錯嗎?

謝謝!卡斯帕

HTML:

<li>What is your sex?<SELECT NAME="sexItem" id="sexItem"></li> 
    <OPTION value="1.25">Male</OPTION> 
    <OPTION value="1.50">Female</OPTION> 

<button type="button" onclick="calculateTime()">Calculate</button> 

<p>You are able to drive at:<INPUT type="text" id="driveTime" Size=1>o'clock.</p> 

JS:

var elt = document.getElementById("sexItem"); 
var sex = elt.options[elt.selectedIndex].value; 

//calculate total value 
var total = amount*sex+time; 

//print value to driveTime 
document.getElementById("driveTime").value=total; 

回答

0
的價值觀

使用parseFloat函數 '性' 返回一個字符串:

var sex = parseFloat(elt.options[elt.selectedIndex].value); 
相關問題