我在HTML文件的標記中使用onChange()命令調用javascript文件中的getPrice()函數,但不管我做了什麼或修復了它,它仍然不顯示價格。我做了很多研究,但似乎找不到任何正確的解決方案。幫助將不勝感激。Javascript - 函數未定義(onChange不起作用)
這裏是我的HTML代碼:
<body>
<form action="#" name="ITSbookform" id="ITSform">
<fieldset>
<legend>Into The Storm Booking</legend>
<label>Choose your cinema</label><br>
<select id="selectedCinema">
<option>--Select--</option>
<option value="maximaCinema">Maxima Cinema</option>
<option value="rivolaCinema">Rivola Cinema</option>
</select><br>
<label>Select day and time</label>
<select id="dayAndTime">
</select>
<br>
<label>Select Seat</label>
<select id="selectedSeat" onchange="getPrice()">
</select>
<p id="total">Total Price: </p>
</form>
</fieldset>
</body>
和javascipt的部分:
function getPrice(){
var totalPrice = 0;
if(document.getElementById("selectedCinema").value == "maximaCinema"){
if(document.getElementById("dayAndTime").value == "9pmMonday" || document.getElementById("dayAndTime").value == "9pmTuesday"){
seatPrice["full"] = 12;
seatPrice["conc"] = 10;
seatPrice["child"] = 8;
seatPrice["FirstClass-Adult"] = 25;
seatPrice["FirstClass-Child"] = 20;
seatPrice["beanbag"] = 20;
}
else{
seatPrice["full"] = 18;
seatPrice["conc"] = 15;
seatPrice["child"] = 12;
seatPrice["FirstClass-Adult"] = 30;
seatPrice["FirstClass-Child"] = 25;
seatPrice["beanbag"] = 30;
}
}
else{
seatPrice["adult"] = 18;
seatPrice["conc"] = 15;
seatPrice["child"] = 12;
}
var seat = theForm.elements["selectedSeat"];
totalPrice = seatPrice[seat.value];
document.getElementById("total").innerHTML = "$" + totalPrice;
}
這裏是代碼中的jsfiddle完整版本:http://jsfiddle.net/stb0v5Ln/
謝謝。
inval 'fieldset'的id html在'form'後關閉 – 2014-10-06 05:34:44
'select'框中沒有任何選項可用,那麼如何改變事件並調用'getPrice()'函數呢? – 2014-10-06 05:38:21
@BhushanKawadkar他正在動態注入它們。 – 2014-10-06 05:47:11