如何計算用戶選擇的兩個下拉菜單的值的總和。我在HTML中沒有預先確定的值。這裏是我的代碼,我想用選定的價格乘以票數,並顯示給用戶。謝謝。下拉菜單計算
HTML
<select name="price" id="price" style="width:120px">
<option>Select Price</option>
</select>
<br>
<br>
<select name="tickets" id="tickets" style="width:120px">
<option>Select Tickets</option>
</select>
<br>
<br>
<input name="button" type="button" value="Submit" onClick="return validate2()"/>
<input name="reset" type="reset" value="Reset" />
</form>
的JavaScript
switch (artist.selectedIndex)
{
case 0:
var venueList = ["Select Venue"];
var dateList = ["Select Date"];
var ticketList = ["Select Tickets"];
var priceList = ["Select Price"];
fillList(venue, venueList);
fillList(date, dateList);
fillList(ticket, ticketList);
fillList(price, priceList);
break;
case 1:
var venueList = ["Select Venue", "London"];
var dateList = ["Select Date", "17th July", "18th July"];
var ticketList = ["Select Tickets", "1", "2", "3", "4", "5", "6"];
var priceList = ["Select Price", "£30", "£45", "£70"];
fillList(venue, venueList);
fillList(date, dateList);
fillList(ticket, ticketList);
fillList(price, priceList);
break;
case 2:
var venueList = ["Select Venue", "Manchester", "Glasgow"];
var dateList = ["Select Date"]
var ticketList = ["Select Tickets", "1", "2", "3", "4", "5", "6"];
var priceList = ["Select Price", "£35", "£50", "£60"];
fillList(venue, venueList);
fillList(date, dateList);
fillList(ticket, ticketList);
fillList(price, priceList);
// adding onchange event on venue when the user selects rod stewart
venue.onchange = function()
{
var dateList;
switch(venue.selectedIndex)
{
case 0: dateList = ["Select Date"]; break;
case 1: dateList = ["Select Date", "18th July", "20th July"]; break;
case 2: dateList = ["Select Date", "22nd July", "23rd July"]; break;
}
fillList(date, dateList);
}
break;
似乎沒有工作。我已經把這個代碼放到我的JavaScript: 功能totalPrice(){ \t VAR truePrice = parseFloat(與string.replace(price.value, '£', '')), noOfTickets = parseInt函數(ticket.value); return document.getElementById(mytotal).innerHTML = truePrice * noOfTickets; } 並在我的HTML中有div標記輸出給用戶 –
看起來你錯過了我做的編輯。我忘了你正在使用選擇框,但在我發佈之後就已經意識到了。看看編輯後的代碼;) – PeteAUK
我得到錯誤代碼:未捕獲TypeError:對象函數String(){[native code]}沒有方法'replace' –