【问题标题】:iOS 6 - Safari - onchange for select element acting strangely (change selectedIndex)iOS 6 - Safari - 选择元素的 onchange 行为异常(更改 selectedIndex)
【发布时间】:2012-09-18 11:33:28
【问题描述】:

这里是:

<select id="test" onchange="alert(this.selectedIndex); this.selectedIndex = 0">
    <option>test1</option>
    <option>test2</option>
    <option>test3</option>
</select>

在所有浏览器中都能正常工作。它在 iOS5 上运行良好。它在 iOS6 中不起作用。它确实显示了警报屏幕,但似乎忽略了重置索引的第二部分。奇怪的是,将 onchange 更改为 onblur 使其可以在 iOS6 Safari 上运行,但会破坏所有其他浏览器。

那么,这是 iOS 错误还是什么?任何帮助表示赞赏。

【问题讨论】:

    标签: javascript safari mobile-safari dom-events


    【解决方案1】:

    添加一个带有 iOS6 用户代理过滤器的 onblur,并在其中设置选定的索引。我确信有比这更好的方法,但它对我有用(这里有一些 jQuery):

    <select id="test" onchange="alert(this.selectedIndex); this.selectedIndex = 0" onblur="FixSelecteOniOS6(this);">
        <option>test1</option>
        <option>test2</option>
        <option>test3</option>
    </select>
    <script>
        FixSelectOniOS6: function (ref){
            alert(ref.selectedIndex)
            var res = navigator.userAgent.match(/; CPU.*OS (\d_\d)/);
            if (res) {
                var strVer = res[res.length - 1];
                strVer = strVer.replace("_", ".");
                version = strVer * 1;
            }
            var i = 0,
            iOS = false,
            iDevice = ['iPad', 'iPhone', 'iPod'];
    
            for (; i < iDevice.length; i++) {
                if (navigator.platform === iDevice[i]) { iOS = true; break; }
            }
            if (navigator.platform.indexOf("iPad") != -1 && version == 6) {
                $(ref).find("option").removeAttr("selected");
                $(ref).find("option:first").attr("selected", "selected");
            }
        }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      相关资源
      最近更新 更多