简体   繁体   English

如何将 HTML 多选 select 框滚动到第一个选中的选项?

[英]How do I scroll an HTML multiple-selection select box to the first selected option?

I am trying to get an HTML multiple-selection select box to scroll to the first selected option, but I cannot find any way of doing so.我试图让 HTML 多选select框滚动到第一个选择的选项,但我找不到任何方法。 Is there a way?有办法吗?

<select id=s multiple> 
<option value=0>One</option>
...

<option value=99 selected>Many</option>
</select>

You can use the below function that will look for the first selected element in your list, and will scroll the select box to it.您可以使用下面的 function 查找列表中第一个选定的元素,并将 select 框滚动到它。

Make sure you call the function when you already have the selections loaded, usually on window.onload event, but if you're populating the selections with ajax call, the function needs to be called respectively (once the selections are already in place)确保在已经加载选择时调用 function,通常是在window.onload事件上,但如果您使用 ajax 调用填充选择,则需要分别调用 function(一旦选择已经到位)


// this is how you call the function, using the ID ("s") in your example:
window.onload = function(){
 scrollToSelected( "s" )
}

function scrollToSelected( select_id ) {
  var select = document.getElementById( select_id );
  var opts = select.getElementsByTagName('option');
  for ( var j = opts.length -1; j > 0; --j ) {
    if ( opts.item(j).selected == true ) {
      select.scrollTop = j * opts.item(j).offsetHeight;
      return;
    } 
  }
}

With the following line you can set the exact scroll position:使用以下行,您可以设置精确滚动 position:

select.scrollTop = j * opts.item(j).offsetHeight;

... the above will attempt to scroll until the selected will be in the top of the list. ...上面的内容将尝试滚动,直到所选内容位于列表顶部。

Thanks for your advice.谢谢你的建议。

It seems that I cannot post code in a comment, so I will need to put this here.看来我不能在评论中发布代码,所以我需要把它放在这里。 What seems to work is height: 20px;似乎有效的是height: 20px; in the CSS for the option tag and the following Javascript:option标签的 CSS 和以下 Javascript 中:

 var select = document.getElementById(select_id);
    if (!select) console.error("no " + select_id);
    var opts = select.getElementsByTagName('option');
    var j = select.selectedIndex;
    if (j == -1) return;
    select.scrollTop = j * 20;

I'm getting 0 for the option's offsetHeight but hard coding a value as above seems to work.我得到的选项的offsetHeight0 ,但如上所述硬编码一个值似乎可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取多选HTML select当前选中项的数组? - How to get an array of currently selected items of a multiple-selection HTML select? 自动填充多个选择框会在Internet Explorer中选择第一个选项 - Autopopulating multiple select box has the first option selected in Internet Explorer 如何在不直接选择选项的情况下将HTML选择框滚动到选项? - How to scroll an HTML select box to an option without directly selecting that option? 如何保留使用JavaScript的多项选择的顺序? - how to reserve the order of multiple-selection using javascript? 选择框不首先显示所选选项 - Select box not showing the selected option first 如何根据“选择”框中的项目选择提供所选属性? - How do I give the selected properties according to the item selection in the Select box? 如何获得在多个选择框中选择的最新选项 - How to get the latest option selected in a multiple select box 在第二个 select 框中禁用第一个 select 框的选定选项 - Disable selected option of the first select box in the second select box javascript-从另一个选择中选择一个选项时,从多个选择框中隐藏选项 - javascript - Hide Options from Multiple Selection Box when an option from another select is selected 如何在选择框中检索所选选项的从零开始的索引? - How do I retrieve the zero-based index of the selected option in a select box?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM