Workaround for bug with 0th <select> option in Mobile Safari on iPad?
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:
- Create a
<select>
element with two or more options. - Clear it by programmatically setting
selectedIndex = -1
or removing the"selected"
attribute from all the<option>
elements. - Tap the select element and choose the 0th option. The selected index won't change.
- 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.
精彩评论