简体   繁体   English

在多个选择之间移动项目

[英]Move items across between multiple select

I have two multi selects viz . 我有两个多重选择, mySelectNumberTest1 and mySelectNumberTest2 . mySelectNumberTest1mySelectNumberTest2 What I want to do is be able to select items in the first dropdown and when I click on a checkbox/button or any control that will trigger an event, it should take the options from mySelectNumberTest1 and populates it into mySelectNumberTest2 . 我想要做的是能够在第一个下拉列表中选择项目,并且当我单击复选框/按钮或将触发事件的任何控件时,它应该采用mySelectNumberTest1的选项并将其填充到mySelectNumberTest2

I have followed this link: Get selected value in dropdown list using JavaScript? 我已点击此链接: 使用JavaScript获取下拉列表中的选定值? its a partial solution to my problem. 这是我的问题的部分解决方案。 The only thing that's missing is that if you select more than one item in mySelectNumberTest1 it will only take the first item and populate it into mySelectNumberTest2 . 唯一缺少的是,如果您在mySelectNumberTest1选择了多个项目,它将仅获取第一个项目并将其填充到mySelectNumberTest2

The below code only works when I am populating items from a select into a normal input textbox. 以下代码仅在将选择中的项目填充到普通输入文本框中时才有效。

 <select id="mySelectNumberTest1" name="mySelectNumberTest" multiple="multiple">
    <option>1</option>
    <option>2</option>
    <option>3</option>
 </select>
<input  type="checkbox" id="tickHereToMove" />
<input id="mySelectNumberTest2" name="mySelectNumberTest2" type="text" onchange="copyTextValueTest(this);" />


function copyTextValueTest(bf) {
    var e = document.getElementById("mySelectNumberTest1");
    var strUser = e.options[e.selectedIndex].text;
    document.getElementById("mySelectNumberTest2").value = strUser;
    alert(strUser);
}

There's also a few problems with this approach as it concatenates the text. 这种方法在连接文本时也存在一些问题。 I tried the .split() function and that still didn't give me the result I wanted. 我尝试了.split()函数,但仍然没有得到我想要的结果。 I want the selected items in each line on mySelectNumberTest2 . 我想要mySelectNumberTest2每一行中的选定项目。

This library http://quasipartikel.at/multiselect/ does EXACTLY what I want to do. 这个库http://quasipartikel.at/multiselect/确实可以实现我想要的功能。 I am working in an Angular JS project and when I try include the jquery scripts in my index.html page, it causes anomalous behaviour and messes up the styling of the page as well. 我正在Angular JS项目中工作,当我尝试在我的index.html页面中包含jquery脚本时,它会导致异常行为,并且还会弄乱页面的样式。

So if I select 1 and 3 in mySelectNumberTest1 , it should populate it in mySelectNumberTest2 in two different lines and not 12 . 因此,如果我在mySelectNumberTest1选择13 ,则应该在mySelectNumberTest2中的两行中而不是12填充它。

If this can be achieved in a drag and drop implementation I would also be happy. 如果这可以通过拖放实现来实现,我也将很高兴。

Simply do this: 只需执行以下操作:

 function addOptions( fromId, toId ) { var fromEl = document.getElementById( fromId ), toEl = document.getElementById( toId ); if ( fromEl.selectedIndex >= 0 ) { var index = toEl.options.length; for ( var i = 0; i < fromEl.options.length; i++ ) { if ( fromEl.options[ i ].selected ) { toEl.options[ index ] = fromEl.options[ i ]; i--; index++ } } } } 
 ul { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: 70px 40px 70px; grid-gap: 10px } select { width: 70px } li.relative { position: relative } #add { position: absolute; width: 40px; font-size: 18px; top: 0 } #remove { position: absolute; width: 40px; font-size: 18px; bottom: 0 } 
 <ul> <li> <select id="select1" name="select1" multiple> <option>Item 1</option> <option>Item 2</option> <option>Item 3</option> <option>Item 4</option> </select> </li> <li class="relative"> <input type="button" id="add" value="&#8702;" onclick="addOptions( 'select1', 'select2' )" /> <input type="button" id="remove" value="&#8701;" onclick="addOptions( 'select2', 'select1' )" /> </li> <li> <select id="select2" name="select2" multiple></select> </li> </ul> 

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

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