在选择框中选择选项重定向 [英] Redirect on select option in select box

查看:159
本文介绍了在选择框中选择选项重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < select ONCHANGE =location = this.options [this.selectedIndex ]。价值;> 

它将我重定向到选项值内的位置。但它不能按预期工作。意思是说,如果我点击select的第一个选项,那么onChange操作就不会运行。我正在考虑JavaScript,但我认为你会有更好的建议家伙。所以,如果我点击每个选项,它会将我重定向到它的值,那么如何使它工作? 解决方案

因为第一个选项已被选中,更改事件永远不会被触发。添加一个空值作为第一个值,并在位置分配中检查是否为空。



以下是一个示例:

 < select onchange =this.options [this.selectedIndex] .value&&(window.location = this.options [this.selectedIndex] .value);> 
< option value =>选择...< / option>
< option value =http://google.com> Google< / option>
< option value =http://yahoo.com> Yahoo< / option>
< / select>

以下是小提琴: http://jsfiddle.net/bL5sq/


At the moment I am using this:

<select ONCHANGE="location = this.options[this.selectedIndex].value;">

it redirect me on the location inside of the option value. But it doesn't work as expected .. mean that if I click on the first option of the select, then onChange action not running. I was thinking about javascript, but I think you'll have some better suggestions guys. So how can I make it work if I click on each option it'll redirect me to the it's value?

解决方案

Because the first option is already selected, the change event is never fired. Add an empty value as the first one and check for empty in the location assignment.

Here's an example:

<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
    <option value="">Select...</option>
    <option value="http://google.com">Google</option>
    <option value="http://yahoo.com">Yahoo</option>
</select>

And here's the fiddle: http://jsfiddle.net/bL5sq/

这篇关于在选择框中选择选项重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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