2016-02-29 33 views
1

我有一個HTML頁面,我有一個下拉菜單和一個按鈕。下拉元素選擇和JS調用不起作用

選擇一個元素後,按下按鈕和我通話功能採取行動,按照下面的代碼:

<!DOCTYPE html> 
<html > 
    <head> 
    <meta charset="UTF-8"> 
    <title>Custom dropdown</title> 
     <link rel="stylesheet" href="cssdropdown/style.css"> 
    </head> 
    <body> 
    <h3>Restart any service from here</h3> 

<!--form method="post" action="example_new.php"--> 
<span class="custom-dropdown"> 
    <select name="restart-component" id="restart-component"> 
     <option value=0>Data 1</option> 
     <option value=1>Data 2</option> 
    </select> 
</span> 

<button id="submit" name="submit" style="background-color:#1abc9c padding-left: 2cm;" onclick='goToRestart();'>Restart</button> 

<p>Legend</p> 
<br/> 

<script type="text/javascript"> 
    function goToRestart() { 
     alert ("into js"); 
     var url; 
     var location = document.getElementById("restart-component").selectedIndex; 
     if (location == 1) { 
      url = 'http://www.page.com/west'; 
     } 
     else if (location == 2) { 
      url = 'http://www.page.com/land'; 
     } 
     #window.location.href=url; 
     alert(url) 
    } 
</script> 

    <script src='http://ajax.googleapis.com/ajax/libs/prototype/1.7.1/prototype.js'></script> 
     <script src="jsdropdown/index.js"></script> 
    </body> 
</html> 

但我的JavaScript函數不會被調用。我錯過了什麼嗎?請幫忙。

+0

任何錯誤? – Lal

+0

如果您可以在[小提琴](https://jsfiddle.net/) – Lal

+1

'type ='button''中重現問題,這將會非常有幫助嗎? '#window.location.href' =>'window.location.href' – Rayon

回答

0

總結你的函數在onLoad()或更改腳本如下

<script type="text/javascript"> 
(function() { 
    // your page initialization code here 
    // the DOM will be available here 
    function goToRestart() { 
    alert("into js"); 
    var url; 
    var location = document.getElementById("restart-component").selectedIndex; 
    if (location == 1) { 
     url = 'http://www.page.com/west'; 
    } else if (location == 2) { 
     url = 'http://www.page.com/land'; 
    } 
    window.location.href = url; 
    alert(url); 
    } 

})(); 
</script> 
+0

仍然收到錯誤:Uncaught ReferenceError:goToRestart未定義 –

+0

瀏覽器控制檯中的錯誤仍然存​​在? – Lal

+0

對不起,在'alert(url)'行中錯過';'...請將它改爲'alert(url);' – Lal

0

嘗試註釋此行第一

//#window.location.href=url;

<script type="text/javascript"> 
    function goToRestart() { 
     alert ("into js"); 
     var url='empty'; 
     var location = document.getElementById("restart-component").selectedIndex; 
     if (location == 1) { 
      url = 'http://www.page.com/west'; 
     } 
     else if (location == 2) { 
      url = 'http://www.page.com/land'; 
     } 
     //#window.location.href=url; //this line 
     alert(url) 
    } 
</script> 

你一定會喜歡用「try catch

0

有你的腳本語法錯誤。嘗試:

function goToRestart() { 
    alert("into js"); 
    var url; 
    var location = document.getElementById("restart-component").selectedIndex; 
    if (location == 0) { 
     url = 'http://www.page.com/west'; 
    } else if (location == 1) { 
     url = 'http://www.page.com/land'; 
    } 
    //#window.location.href=url; line with syntax error on the character # 
    window.location.href = url; 
    alert(url); 

然後從onclick='goToRestart();'您的功能改變調用只是onclick='goToRestart()'

希望它可以幫助您的瀏覽器控制檯