2015-11-03 28 views
0

我在網站中做了一個帶有鏈接的下拉菜單,但是當我選擇該項目並點擊開始時,它不會在任何地方重定向。你能分享下面的代碼中的錯誤嗎?如何使用鏈接進行下拉菜單

<script> 
     function goToNewPage(dropdownlist){ 
     var url = dropdownlist.options(dropdownlist.selectedIndex).value; 
     if (url != ""){ 
     window.open(url); 
     }} 
    </script> 
    <form name="dropdown"> 
     <label>I am Looking for</label><img src="http://test.techkalph.com/wp-content/uploads/2015/10/Bee-searching1.png"> 
     <select name="list" accesskey="E"> 
     <option selected>Please select one</option> 
     <option value="http://www.google.com/" style="background-image:url(http://test.techkalph.com/wp-content/uploads/2015/10/flowers271.png); background-repeat:no-repeat;">Japanese Companies</option> 
     <option value="http://www.google.com/" style="background-image:url(http://test.techkalph.com/wp-content/uploads/2015/10/social16.png); background-repeat:no-repeat;">Service Provider (Non-Japanese)</option> 
     <select> 
     <input type=button value="Go" onclick="goToNewPage(document.dropdown.list)"> 
    </form> 

期待您的回覆。

謝謝。

回答

3

替換此:

var url = dropdownlist.options(dropdownlist.selectedIndex).value; 

本:

var url = dropdownlist.options[dropdownlist.selectedIndex].value; 

聲明的方括號。 dropdownlist.options返回一個數組,並選擇一個索引,我們在Javascript中使用方括號。

錯誤在於您使用了圓括號而不是方括號

Here is the JSFiddle demo

0

你可以嘗試在你的select上添加一個id,並用jquery添加一個事件onchange。

<select id="gotourl"> 
    <option selected>Please select one</option> 
           <option value="http://www.google.com/" style="background-image:url(http://test.techkalph.com/wp-content/uploads/2015/10/flowers271.png); background-repeat:no-repeat;">Japanese Companies</option> 
           <option value="http://www.google.com/" style="background-image:url(http://test.techkalph.com/wp-content/uploads/2015/10/social16.png); background-repeat:no-repeat;">Service Provider (Non-Japanese)</option> 
           <select> 

JQuery的:

$("#gotourl").change(function(){ 
if($(this).val()!="" && $(this).val()!=null) 
{ 
    window.location.href=$(this).val(); 
} 
}); 
1

我發現創建一個跳轉菜單的最簡單的方法是複製並粘貼代碼,然後進行必要的修改。 Jump Menus

JavaScript代碼:

<script language="JavaScript"> 
<!-- 
function MM_jumpMenu(targ,selObj,restore){ //v3.0 
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); 
if (restore) selObj.selectedIndex=0; 
} 
//--> 
</script> 

HTML代碼:

<select name="select" onChange="MM_jumpMenu('parent',this,1)"> 
     <option value="http://www.google.com" selected>Google</option> 
     <option value="http://www.bing.com">Bing</option> 
     <option value="http://www.yahoo.com">Yahoo</option> 
     <option value="http://www.facebook.com">Facebook</option> 
    </select> 

編輯:

在這個例子中,你就不需要提交/去按鈕。