开发者

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

开发者 https://www.devze.com 2023-03-26 20:04 出处:网络
In Mobile Safari on the iPad, it seems that if there is no option selected on a <select> element, then the user cannot select the 0th option before selecting another one first (try it on http://

In Mobile Safari on the iPad, it seems that if there is no option selected on a <select> element, then the user cannot select the 0th option before selecting another one first (try it on http://jsfiddle.net/PJTKq/ on an iPad).

To be more specific:

  1. Create a <select> element with two or more options.
  2. Clear it by programmatically setting selectedIndex = -1 or removing the "selected" attribute from all the <option> elements.
  3. Tap the select element and choose the 0th option. The selected index won't change.
  4. Tap the select element and choose another option, then tap it again and choose the 0th option. The selected index should change tw开发者_JS百科ice.

Does anyone know of a workaround (besides inserting an empty dummy option) that would allow moving directly from selectedIndex -1 to selectedIndex 0?


<script>    
// with jQuery
var iPad = !!navigator.userAgent.match(/iPad/i),
    select = "select";

if(iPad === true) {  
    $(select).prop("selectedIndex", 0);
}
</script>


Until the bug is fixed, perhaps you could detect the user-agent and make selectedIndex=0 the default for Mobile Safari.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号