简体   繁体   English

如何使用链接进行下拉

[英]How to make dropdown with link

I have made a dropdown with link in my site but when I select the item and click go, it is not redirecting anywhere. 我在站点中添加了带有链接的下拉列表,但是当我选择该项目并单击“转到”时,它不会重定向到任何地方。 Can you please share what is the error in my below code? 您能在下面的代码中分享什么错误吗?

    <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>

Looking forward for your reply. 期待您的答复。

Thank you. 谢谢。

Replace this: 替换为:

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

by this: 这样:

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

Notice the square brackets. 注意方括号。 dropdownlist.options returns an array, and to select an index we use square brackets in Javascript. dropdownlist.options返回一个数组,要选择索引,我们在Javascript中使用方括号。

The mistake was that you used round-brackets instead of square brackets . 错误是您使用了圆括号而不是方括号

Here is the JSFiddle demo 这是JSFiddle演示

I've found the easiest way to create a jump menu is to copy and paste the code and then make the changes necessary. 我发现创建跳转菜单的最简单方法是复制并粘贴代码,然后进行必要的更改。 Jump Menus 跳转菜单

Javascript Code: 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 Code: 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>

Edit: 编辑:

For this example, you wouldn't need a submit/go button. 对于此示例,您不需要提交/执行按钮。

you can try to add an id on your select and add an event onchange with jquery. 您可以尝试在您的选择上添加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 : jQuery的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM