javascript - jQuery how to select the first non-hidden select option -
the following behaves differently between jquery 1.9 , 1.10+:
<select id="s1"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> $('#s1 option[value=1]').hide(); $('#s1').val('');
the idea behind code select first non-hidden option, after hiding options, including selected one.
since jquery 1.10+ $('#s1').val('');
no longer selects first non-hidden option, while .val(<some proper value particular select box>)
works ok.
trying following approaches not because both selectedindex
, .first().val()
consider hidden options:
$("#s1").prop("selectedindex", 0);
$('#s1 option').first().prop('selected', true);
next thing comes mind (also suggested c-link) not work, because :visible
selector not work select options.
$('#s1 option:visible').first().prop('selected', true);
looking generic way (not depending on knowledge of particular values , options have been hidden) achieve same behaviour $('#s1').val('');
in old jquery.
shortly this:
$('#s1').find("option:not(:hidden):eq(0)");
Comments
Post a Comment