简体   繁体   中英

scroll to selected item in html listbox

I have an html listbox :

<select id="test" size="5">           
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>

I am trying to select an item via code and have the listbox automatically scroll to the selected item.

Is this supposed to scroll the listbox automatically to the selected item ?

$('#test option[value="8"]').attr('selected', 'selected');

I can't seem to get it to do that ... am I missing something ?

Setting the selected item will only cause the item to be visible within the select field, but doesn't guarantee that it will be at the top of the list of items shown. I tried this in a jsfiddle and you'll see your code highlights the #8, but it's at the bottom of the select field box. http://jsfiddle.net/Ck4m3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM