开发者

Select option in select box will pass to another website JS

开发者 https://www.devze.com 2023-01-23 01:43 出处:网络
I am not JS expert开发者_运维问答.. I want when I select a < option> in < select> it will pass the user to another page.

I am not JS expert开发者_运维问答.. I want when I select a < option> in < select> it will pass the user to another page.

How can I do it?


If you just want to change the url whenever an option is selected, you could use something like this:

<select onChange="javascript:document.location=this.value;">
<option value='http://google.com'>Google</option>
<option value='http://stackoverflow.com'>Stack Overflow</option>
<option value='http://microsoft.com'>Microsoft</option>
</select>


You should have something like that:

<select id="theSelect">
  <option value="/pathTo/page1.html">page 1</option>
  <option value="/pathTo/page2.html">page 2</option>
</select>

Then on the select set an onchange event:

var sel = document.getElementById('theSelect');
sel.onchange = function(e){
  window.location.href = sel.options[sel.selectedIndex].value;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消