Javascript 根据选择选项元素将表单重定向到不同的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12388954/
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
Redirect form to different URL based on select option element
提问by user1400854
new to JavaScript seeking some help. I have a form with a select drop down with 5 options. Option1 Option2 option3 Option4 Option5'
JavaScript 新手寻求帮助。我有一个带有 5 个选项的选择下拉列表的表单。选项 1 选项 2 选项 3 选项 4 选项 5'
I need to have the form to redirect to another url if any of the options are selected apart from Option 1 which should be the default one on page load.
如果除了选项 1 之外的任何选项被选中,我需要将表单重定向到另一个 url,选项 1 应该是页面加载时的默认选项。
Thank you in advance
先感谢您
I only used the following
我只使用了以下
<form action="" id="main" name="main" method="get" onChange="top.location.href=this.options[this.selectedIndex].value;" value="GO">
<select id="Region" name="Region" tabindex="7">
<option value="/url">Option1</option>
<option value="/url">Option2</option>
<option value="/url">Option3</option>
<option value="/url>Option4</option>
<option value="" selected="selected">Option5</option>'
</select>
回答by Krishna Kumar
Just use a onchnage Event
for select box.
只需使用一个onchnage Event
for 选择框。
<select id="selectbox" name="" onchange="javascript:location.href = this.value;">
<option value="https://www.yahoo.com/" selected>Option1</option>
<option value="https://www.google.co.in/">Option2</option>
<option value="https://www.gmail.com/">Option3</option>
</select>
And if selected option to be loaded at the page load then add some javascript code
如果选择在页面加载时加载的选项,则添加一些 javascript 代码
<script type="text/javascript">
window.onload = function(){
location.href=document.getElementById("selectbox").value;
}
</script>
for jQuery: Remove the onchange event from <select>
tag
对于 jQuery:从<select>
标记中删除 onchange 事件
jQuery(function () {
// remove the below comment in case you need chnage on document ready
// location.href=jQuery("#selectbox").val();
jQuery("#selectbox").change(function () {
location.href = jQuery(this).val();
})
})
回答by kumbhanibhavesh
you can use this simple way
你可以用这个简单的方法
<select onchange="location = this.value;">
<option value="/finished">Finished</option>
<option value="/break">Break</option>
<option value="/issue">Issues</option>
<option value="/downtime">Downtime</option>
</select>
will redirect to route url you can direct to .html page or direct to some link just change value
in option.
将重定向到路由 url,您可以直接到 .html 页面或直接到某个链接,只需更改value
选项即可。
回答by Codemaker
This can be archived by adding code on the onchange event of the select control.
这可以通过在选择控件的 onchange 事件上添加代码来存档。
For Example:
例如:
<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<option value="http://gmail.com">Gmail</option>
<option value="http://youtube.com">Youtube</option>
</select>