Workaround for bug with 0th <select> option in Mobile

Workaround for bug with 0th <select> option in Mobile Safari on iPad?

本文关键字:select gt option Mobile in lt for bug with 0th Workaround      更新时间:2023-09-26

在iPad上的Mobile Safari中,如果在<select>元素上没有选择选项,那么用户在选择另一个元素之前无法选择第0个选项(在iPad上的http://jsfiddle.net/PJTKq/上尝试一下)。

更具体地说:

  1. 创建具有两个或多个选项的<select>元素。
  2. 通过编程设置selectedIndex = -1或从所有<option>元素中移除"selected"属性来清除它。
  3. 点击选择元素,选择第0个选项。选择的索引不会改变。
  4. 点击选择元素并选择另一个选项,然后再次点击并选择第0个选项。所选索引应更改两次。

有没有人知道一个解决方案(除了插入一个空的虚拟选项),将允许直接从selectedIndex -1移动到selectedIndex 0?

<script>    
// with jQuery
var iPad = !!navigator.userAgent.match(/iPad/i),
    select = "select";
if(iPad === true) {  
    $(select).prop("selectedIndex", 0);
}
</script>

在这个错误被修复之前,也许您可以检测用户代理并将selectedIndex=0设置为Mobile Safari的默认值。