Javascript iOS 选择 onchange 不触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8004227/
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
iOS select onchange not firing
提问by George Kendros
Does anyone know any workarounds for this? I'm trying to use a select element for navigation. It looks something like this;
有没有人知道任何解决方法?我正在尝试使用 select 元素进行导航。它看起来像这样;
<select onchange="window.location=(this.options[this.selectedIndex].value)">
It works on all desktop browsers and it also works on my WP7 phone. It doesn't work on my iPhone (iOS 5.0).
它适用于所有桌面浏览器,也适用于我的 WP7 手机。它不适用于我的 iPhone (iOS 5.0)。
回答by Paul Irish
iOS tends to not fire change
events when you change the value of their inputs. I've documented this in more depth at the Device-Bugs tracker.
change
当您更改其输入值时,iOS 往往不会触发事件。我已经在 Device-Bugs tracker 中更深入地记录了这一点。
The workaround is to bind to the blur
event instead. In the above example, use onblur
and you'll be all set.
解决方法是改为绑定到blur
事件。在上面的例子中,使用onblur
你就可以了。
<select onblur="window.location=(this.options[this.selectedIndex].value)">
回答by Danny
If you change onchange
to onblur
it works for iOS but not others. The fix I eventually came to was to use BOTH onblur
and onchange
. Seems redundant but has the desired effect.
如果您更改onchange
为onblur
它适用于iOS,但不适用于其他人。我最终找到的解决方法是同时使用 BOTHonblur
和onchange
. 看似多余,却达到了预期的效果。
回答by George Kendros
I just ended up setting the form action to a php page that redirects the page. You don't even need a submit button (although you may as well add one for graceful degradation then hide it with js), the iOS list picker widget automatically triggers the form action when it's 'Done' button is pressed.
我刚刚结束将表单操作设置为重定向页面的 php 页面。您甚至不需要提交按钮(尽管您也可以添加一个用于优雅降级然后用 js 隐藏它),当按下“完成”按钮时,iOS 列表选择器小部件会自动触发表单操作。
I originally wanted to avoid form actions and submit buttons because I felt the js onChange approach was cleaner, but I'm fine with using php on iOS since it doesn't require the submit button.
我最初想避免表单操作和提交按钮,因为我觉得 js onChange 方法更干净,但我可以在 iOS 上使用 php,因为它不需要提交按钮。