<html>
<head>
<title>Tip Calculator</title>
<script type="text/javascript"><!--
function calculateBill(){
var check = document.getElementById("check").value;
/* I try to get the value selected */
var tipPercent = document.getElementById("tipPercent").value;
/* But it always returns the value 15 */
var tip = check * (tipPercent/100)
var bill = 1 * check + tip;
document.getElementById('bill').innerHTML = bill;
}
--></script>
</head>
<body>
<h1 style="text-align:center">Tip Calculator</h1>
<form id="f1" name="f1">
Average Service: 15%
<input type="radio" id="tipPercent" name="tipPercent" value="15" />
<br />
Excellent Service: 20%
<input type="radio" id="tipPercent" name="tipPercent" value="20" />
<br /><br />
<label>Check Amount</label>
<input type="text" id="check" size="10" />
<input type="button" onclick="calculateBill()" value="Calculate" />
</form>
<br />
Total Bill: <p id="bill"></p>
</body>
</html>
我試圖讓與document.getElementById("tipPercent").value
選擇的價值,但它始終返回值15如何讀取JavaScript中單選按鈕的值?
歡迎來到SO。請提供一些你想做什麼,哪些不起作用,以及你已經嘗試過的東西的解釋。投票結束。 –
你介意寫下你的問題而不是粘貼整個HTML代碼嗎? –
這就像讓你的母親髒盤子期待她爲你清理它。 –