2015-10-30 51 views
1

我的javascript函數有問題返回正確的值。所以基本上,我預計會出現的是tour_prices-arraylist的價格之一。但實際上什麼都沒有出現。任何幫助表示讚賞。使用javascript函數返回值時出現問題

var tour_prices = new Array(); 
 
tour_prices["0"]=3000; 
 
tour_prices["1"]=4400; 
 
tour_prices["2"]=5500; 
 
tour_prices["3"]=6600; 
 
tour_prices["4"]=7700; 
 

 
function tourPrice() { 
 
    var thePrice = 0; 
 
    var scheme = document.forms["orderScheme"]; 
 
    var pickDestination = scheme.elements["destination"]; 
 
    thePrice = tour_prices[pickDestination.value]; 
 

 
    return thePrice; 
 
} 
 

 
function calculateTotal() { 
 
    var totalTourPrice = tourPrice(); 
 
    
 
    var totalPrice = document.getElementById('totalPrice'); 
 
    totalPrice.innerHTML = "the total tour price is " + totalTourPrice + " usd."; 
 
}
<form action="" id="orderScheme" onsubmit="return false"> 
 
    <div> 
 
    <fieldset> 
 
    <legend>Order cruise!</legend> 
 
    <b><label>Pick Destination: </label></b> 
 
    <select id="destination" name='destination' onchange="calculateTotal()"> 
 
     <option value="0">destination 1</option> 
 
     <option value="1">destination 2</option> 
 
     <option value="2">destination 3</option> 
 
     <option value="3">destination 4</option> 
 
     <option value="4">destination 5</option> 
 
    </select> 
 
    </fieldset> 
 
    <div id="totalPrice" name="totalPrice"></div><br> 
 
\t \t \t \t \t \t \t 
 
<input type='submit' id='submit' value='calculate' onclick="calculateTotal()"></input> 
 
</form>

+0

'tourPrice()();'< - 錯誤 – epascarello

+0

是啊,是我不好。這不是在實際的代碼tho :) – nilsenfacE

+0

該代碼的工作原理如下,問題在哪裏? – RobG

回答

0

select沒有它自己的value - 你需要找到所選option(通過selectedIndex屬性),那麼得到價值。

var tour_prices = new Array(); 
 
tour_prices["0"]=3000; 
 
tour_prices["1"]=4400; 
 
tour_prices["2"]=5500; 
 
tour_prices["3"]=6600; 
 
tour_prices["4"]=7700; 
 

 
function tourPrice() { 
 
    var thePrice = 0; 
 
    var scheme = document.forms["orderScheme"]; 
 
    var pickDestination = scheme.elements["destination"]; 
 
    var val = pickDestination.options[ pickDestination.selectedIndex ].value; 
 
    thePrice = tour_prices[val]; 
 

 
    return thePrice; 
 
} 
 

 
function calculateTotal() { 
 
    var totalTourPrice = tourPrice(); 
 
    
 
    var totalPrice = document.getElementById('totalPrice'); 
 
    totalPrice.innerHTML = "the total tour price is " + totalTourPrice + " usd."; 
 
}
<form action="" id="orderScheme" onsubmit="return false"> 
 
    <div> 
 
    <fieldset> 
 
    <legend>Order cruise!</legend> 
 
    <b><label>Pick Destination: </label></b> 
 
    <select id="destination" name='destination' onchange="calculateTotal()"> 
 
     <option value="0">destination 1</option> 
 
     <option value="1">destination 2</option> 
 
     <option value="2">destination 3</option> 
 
     <option value="3">destination 4</option> 
 
     <option value="4">destination 5</option> 
 
    </select> 
 
    </fieldset> 
 
    <div id="totalPrice" name="totalPrice"></div><br> 
 
\t \t \t \t \t \t \t 
 
<input type='submit' id='submit' value='calculate' onclick="calculateTotal()"></input> 
 
</form>

+0

Paul Roub:非常感謝! – nilsenfacE

+1

「*選擇沒有自己的值*」不正確。 select元素的值是所選選項的值。如果未選擇任何選項,則值將爲「」(空字符串)。由於關於Netscape 5,使用* selectedIndex *屬性並不是必需的,但它仍然作爲某種模因存在。 – RobG

+0

從Netscape 2開始就一直處於這種狀態,某些習慣仍然存在。當然,你是正確的,任何隱約現代的東西。錯字似乎是真正的問題。 –