简体   繁体   English

自动选择 <option>添加到<select>

[英]Auto select <option> added to <select>

I add "option"s to "select" with javascript or jquery. 我用javascript或jquery将“选项”添加到“选择”中。

I would like to select last added and scroll the scrollbar to most bottom. 我想选择最后添加的内容并将滚动条滚动到最底部。

i add text like this 我添加这样的文字

function ConsoleOutputAdd(text){
    var content = '<option>'+ text +'</option>';
    $consoleoutput.append(content); 
}

this is the select element. 这是选择元素。

<select id="consoleoutput" size="4">></select>

i think it would be with onchanged event but i cant think anything more. 我认为这将与变化的事件,但我不能再想任何其他的事情了。

thank you 谢谢

$('#consoleoutput').find('option:last').prop('selected', true ); 

这是一个小提琴

You can select the last item in the list like so: 您可以像这样选择列表中的最后一项:

var sel = document.getElementById('consoleoutput');
sel.selectedIndex = sel.options.length - 1;

1: write an on-change listener to your select element 1:为您的select元素编写一个不断变化的侦听器

$('#consoleoutput').change(function(){
   // scroll
})

2: add this to select the option you have added: 2:添加此以选择您添加的选项:

$("#consoleoutput").val(text);

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

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