简体   繁体   English

上/下移动时将选择的选项保留在窗口中

[英]Keep selected option in window when moving up/down

I have a fiddle here http://jsfiddle.net/BB3JK/47/ that I adapted from this previous question ( How can I move selected option in multiselect up and down by buttons using jquery? ). 我在这里http://jsfiddle.net/BB3JK/47/提了个小提琴,该小提琴是我根据先前的问题改编而成的( 如何使用jquery通过按钮上下移动多选中的选定选项? )。

I'm writing my code in AngularJS actually, but the issue is more basic I believe. 我实际上是用AngularJS编写代码,但是我认为这个问题更基本。 I have a select box that is 10 high: 我有一个10高的选择框:

<select multiple id="selectedList" size="10">
    <option value="0">Chicago</option>
    <option value="1">Boston</option>
    <option value="2">Dallas</option>
    <option value="3">Atlanta</option>
    <option value="4">Yellow</option>
    <option value="5">Denver</option>
    <option value="6">Nashville</option>
    <option value="7">Orange</option>
    <option value="8">Baltimore</option>
    <option value="9">Crimson</option>
    <option value="10">Seattle</option>
    <option value="11">Orlando</option>
    <option value="12">Memphis</option>
    <option value="13">Cleveland</option>
    <option value="14">Phoenix</option>
    <option value="15">Amber</option>
</select>
<input type="button" value="Move Up">
<input type="button" value="Move Down">

And I need to be able to move options up and down using buttons. 而且我需要能够使用按钮上下移动选项。 That's all working, but when I move an option from the top down to the bottom, it eventually disappears below the select box (ie. select Denver and then continually click Move Down) and I want the select box to scroll down and keep the selected option in view. 一切正常,但是当我将选项从上向下移动到底部时,它最终消失在选择框下方(即,选择“丹佛”,然后连续单击“下移”),我希望选择框向下滚动并保持选中状态选项。

The odd thing is that Move Up works (ie. select Amber at the bottom and Move Up), but Move Down doesn't! 奇怪的是,“向上移动”有效(即,在底部选择“琥珀色并向上移动”),但“向下移动”无效!

I've tried using jQuery with ScrollTop and AngularJS directives and I haven't found anything that works yet. 我尝试将jQuery与ScrollTop和AngularJS指令一起使用,但尚未找到任何有效的方法。

You can use javascript to scroll to the selected option: 您可以使用javascript滚动到选定的选项:

document.getElementById("mySelect").selectedIndex = 6;

I don't know how the code that moves the items looks like but maybe you can insert that line in there somewhere. 我不知道移动项目的代码是什么样的,但是也许您可以在那儿插入那行。

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

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