Html 如何使用javascript从select标签中获取选定的值并使用该值转到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19197309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:07:37  来源:igfitidea点击:

How to get selected value from select tag using javascript and go to another page using that value

javascripthtmlselecthyperlink

提问by puneetjava

I have a select tag where it contains some values, as shown below:

我有一个 select 标签,其中包含一些值,如下所示:

         <select id="menu" SIZE=6 onChange="go()">
          <option value="">Select city</option>
    <option value="delhi" >delhi</option>  
    <option value="kolkata" >kolkata</option>  
    <option value="mumbai" >mumbai</option>
           </select>

Now i am using below script for this, where it get the selected value from the drop down,

现在我为此使用下面的脚本,它从下拉列表中获取选定的值,

      <script>
        function go(){

var sel = document.getElementById('menu');
       var sv = sel.options[sel.selectedIndex].value;

              //  here i  need to make a specific link using this selected drop down value


          }

          </script>

I just need to know how can i make use of this selected value and go to specific link like

我只需要知道如何使用此选定值并转到特定链接,例如

       window.location.href='getCityDetails.jsp?c=sv'; // this is not working properly 

Can anyone suggest me best solution for this.

谁能建议我最好的解决方案。

回答by Neeraj Dubey

 <script>
    function go(){

   var sel = document.getElementById('menu');
   var sv = sel.options[sel.selectedIndex].value;
    window.location.href='getCityDetails.jsp?c=' + sv;

      }

      </script>

Hope it helps you

希望对你有帮助

回答by Oki Erie Rinaldi

HTML :

HTML :

<select id="menu" SIZE=6 onChange="go(this.value)">
    <option value="">Select city</option>
    <option value="delhi" >delhi</option>  
    <option value="kolkata" >kolkata</option>  
    <option value="mumbai" >mumbai</option>
</select>

Javascript :

Javascript :

function go(desination){
    if (destination != ''){
         window.location.href='getCityDetails.jsp?c=' + desination;
    }
}

回答by Rahul Sawant

-- You can use--

- 您可以使用 -

var TaskType = document.form1.SelTaskType.value;

-- TaskType will display selected option from below

-- TaskType 将显示从下方选择的选项

<select id="SelTaskType" name="SelTaskType" >
<option selected>-- select --</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>

回答by Andrei C

you need to concatenate the string properly. try this:

您需要正确连接字符串。尝试这个:

window.location.href='getCityDetails.jsp?c=' + sv;