根据select选项元素将表单重定向到不同的URL [英] Redirect form to different URL based on select option element

查看:228
本文介绍了根据select选项元素将表单重定向到不同的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript的新手寻求一些帮助。我有一个带有5个选项的选择下拉列表。

Option1
Option2
option3
Option4
Option5'

new to JavaScript seeking some help. I have a form with a select drop down with 5 options. Option1 Option2 option3 Option4 Option5'

I如果除了选项1之外选择任何选项时,需要将表单重定向到另一个URL,这应该是页面加载时的默认选项。

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.

提前谢谢

我只使用以下

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


推荐答案

只需使用 onchnage选择框的事件

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

如果选择了要加载的选项页面加载然后添加一些JavaScript代码

And if selected option to be loaded at the page load then add some javascript code

<script type="text/javascript">
    window.onload = function(){
        location.href=document.getElementById("selectbox").value;
    }       
</script>

jQuery的

:从< select> 标签中删除onchange事件

for jQuery: Remove the onchange event from <select> tag

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();
    })
})

这篇关于根据select选项元素将表单重定向到不同的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆