私はあなたがそれを別の方向に向けるべきだと思います:
<div>
<h1>Buy a new bed now!</h1>
<p>
<label for="size">Size</label><br />
<select id="size">
<option value="twin">Twin</option>
<option value="full">Full</option>
<option value="queen">Queen</option>
<option value="king">King</option>
</select>
</p>
<p>
<label for="headBoard">Head Board</label><br />
<select id="headBoard">
<option value="none">None</option>
<option value="modelA">Model A</option>
<option value="modelB">Model B</option>
<option value="modelC">Model C</option>
</select>
</p>
<p>
<input type="button" onclick="buy()" value="Buy!" />
</p>
</div>
次に、次の JavaScript を含めます
var pricing = {
size: {
twin: 100,
full: 200,
queen: 300,
king: 400
},
headBoard: {
none: 0,
modelA: 100,
modelB: 200,
modelC: 300
}
};
function buy() {
var sizeSelection = document.getElementById("size").value,
headBoardSelection = document.getElementById("headBoard").value,
sizePrice = pricing.size[sizeSelection],
headBoardPrice = pricing.headBoard[headBoardSelection],
grandTotal = sizePrice + headBoardPrice;
alert("Grand Total: $" + grandTotal.toFixed(2));
}