问题描述
< select ONCHANGE =location = this.options [this.selectedIndex ]。价值;>
它将我重定向到选项值内的位置。但它不能按预期工作。意思是说,如果我点击select的第一个选项,那么onChange操作就不会运行。我正在考虑JavaScript,但我认为你会有更好的建议家伙。所以,如果我点击每个选项,它会将我重定向到它的值,那么如何使它工作? 解决方案
< 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>
以下是小提琴:
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/
这篇关于在选择框中选择选项重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!