admin管理员组文章数量:1420181
I have a select box with years. First option is empty. When I click on select list I want scrollbar to scroll automaticaly down . I've tried with scrollTop but it's working only in FireFox.
Ex:
<select id="mySelect">
<option selected="selected" value=""></option>
<option value="1911">1911</option>
<option value="1912">1912</option>
<option value="1913">1913</option>
<option value="1914">1914</option>
<option value="1915">1915</option>
<option value="1916">1916</option>
<option value="1917">1917</option>
<option value="1918">1918</option>
<option value="1919">1919</option>
<option value="1920">1920</option>
<option value="1921">1921</option>
<option value="1922">1922</option>
...
</select>
I have a select box with years. First option is empty. When I click on select list I want scrollbar to scroll automaticaly down . I've tried with scrollTop but it's working only in FireFox.
Ex:
<select id="mySelect">
<option selected="selected" value=""></option>
<option value="1911">1911</option>
<option value="1912">1912</option>
<option value="1913">1913</option>
<option value="1914">1914</option>
<option value="1915">1915</option>
<option value="1916">1916</option>
<option value="1917">1917</option>
<option value="1918">1918</option>
<option value="1919">1919</option>
<option value="1920">1920</option>
<option value="1921">1921</option>
<option value="1922">1922</option>
...
</select>
Share
Improve this question
asked Jul 11, 2011 at 15:49
Iulian BoiculeseIulian Boiculese
3731 gold badge6 silver badges16 bronze badges
3
- 3 Why not just reverse the order of the dates? :) – SeanCannon Commented Jul 11, 2011 at 15:52
- A little more explanation, please? Do you want the dropdown to scroll to the bottom of the list? If so, why not invert the order so the last one is on the top? – Andy_Vulhop Commented Jul 11, 2011 at 15:54
- I just want to scroll down the vertical scrollbar with 400px for example without selecting any option because I want the user to see most important options from select list before he selects an option – Iulian Boiculese Commented Jul 11, 2011 at 17:53
5 Answers
Reset to default 3Select click/focus is a bit more plicated, actually. This is what you want:
$('#mySelect').focus(function(){
if($(this).data('focused') !== true)
{
$(this).data('focused',true);
$(this).children(':last-child').prop('selected',true);
}
});
$('#mySelect').blur(function(){
$(this).data('focused',false);
});
Working example: http://jsfiddle/AlienWebguy/zuJrK/
Try the below code I hope it helps
$("#mySelect").click(function(){
$(this).find("option").eq(someMiddleNumber).focus().blur();
});
You may try to remove the style "overflow-y:auto;" from css, that will keep the selected item scroll into the view (when you use arrow keys to change the selection).
Some improvement to AlienWebguy's answer:
You can make sure the default value will stay selected in case user does not make any changes by adding the settimeout section below:
if($(this).data('focused') !== true)
{
$(this).data('focused',true);
$(this).children(':last-child').prop('selected',true);
// dirty workaround
window.setTimeout(function(){
$(this).val("");
}, 50);
}
ShankarSangoli was close but I think you want:
$("#mySelect").click(function(){
$(this).find("option:last").attr("selected","selected");
});
本文标签: javascriptSelect list scroll to a specific option elementStack Overflow
版权声明:本文标题:javascript - Select list scroll to a specific option element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745326546a2653618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论