2016-04-22 131 views
0

我試圖訪問Update_Price函數JavaScript文件時,我做任何更改select標記,但不幸的是未捕獲的參考錯誤出現。這是我的代碼:未捕獲的ReferenceError:函數沒有與onchange定義

<!Doctype html> 
<html> 
    <head> 
    <script type="text/javascript" src="Form-validation.js"></script> 
    </script> 
    </head> 
    <body> 
    <select class="Booking-Form-Group" id="combo-box" onchange="Update_Price()"> 
      <option value="1">Single Room</option> 
      <option value="2">Double Room</option> 
      <option value="3">Twin Room </option> 
      <option value="4">Family Room</option> 
      <option value="5">Standard Room</option> 
      <option value="6">Meeting Hall</option> 
      <option value="7">Conference Room</option> 
     </select> 
    </body> 
</html> 

這是我的JavaScript代碼。

function Update_Price(){ 

    alert('Warning'); 

    var combo = document.getElementById('combo-box'); 
    var index = combo.options[combo.selectedIndex].value; 

    var value = 0; 
    if (index == 1){ 
     value = 15; 
    } 
    else if(index == 2 || index == 3){ 
     value = 25; 
    } 
    else if(index == 4){ 
     value = 40; 
    } 
    else if(index == 5){ 
     value = 20; 
    } 
    else{ 
     value = 60; 
    } 
    alert(value); 

    document.getElementById('price').innerHTML='Total Price is: '.value; 

} 
+0

是'形式,validation.js'裏面的JS代碼? – gurvinder372

+0

更改'document.getElementById('price')。innerHTML ='總價格:'.value;'到'document.getElementById('price')。innerHTML ='總價格:'+ value;' – Gintoki

+0

把你的'腳本'標記到'body'的底部 –

回答

3

你的問題是此行

document.getElementById('price').innerHTML='Total Price is: '.value; 

它應該有一個連接運算符的+代替.

document.getElementById('price').innerHTML='Total Price is: ' + value; 
+1

這是一個問題,但它不是導致錯誤的原因。 –

+2

@安迪你是對的。看起來好像'Update_Price'沒有在全局級定義。等待OP更新 – gurvinder372

相關問題