简体   繁体   English

在选择框中停止对HTML选项的自动排序

[英]Stop automatic sorting of HTML options in select box

I have two HTML select boxes. 我有两个HTML选择框。 I am moving options from one box to the other, box "from[]" and box "to[], respectively. The problem I have is that, the "to[]" box sorts my options alphabetically as they are added. I want each option to be appended to the box and have no sorting at all. 我正在将选项分别从一个框移动到另一个框,分别是“ from []”框和“ to []框”。我的问题是,“ to []”框将添加的选项按字母顺序排序。希望将每个选项附加到框中,并且完全没有排序。

I have done a lot of reading on this and it seems like the "to[]" box should keep the order of the options as they are added but it does not. 我对此进行了大量阅读,似乎“ to []”框应在添加选项时保持选项的顺序,但事实并非如此。

I left my commented out code in my Javascript just you can see what I have tried. 我将注释掉的代码留在Javascript中,以便您可以看到我尝试过的内容。 I'll remove it if it is making everything too messy. 如果将所有内容弄得太乱了,我将其删除。

Thanks! 谢谢!

HTML: HTML:

<select name="from[]" id="fabricBox" class="form-control" size="8" multiple="multiple" style="width:100%">

<select name="to[]" id="fabricBox_to" class="form-control" size="8" multiple="multiple" required></select>

Javascript: Javascript:

$('#fabricBox').dblclick (function(){
                $value = $('#fabricBox option:selected').val();
                $('#fabricBox_to').append($value); 

// $('#fabricBox_to').val() = $value 

// $text = $('#fabricBox option:selected').text();

// $("#fabricBox_to").append($('<option></option>').attr("value",$value).text($text)); 

我无法对其进行测试,但是类似这样的方法应该起作用:

$("#fabricBox_to option:last").append('<option>' + $value + '</option>');

You can do this in four steps: 您可以通过四个步骤执行此操作:

1) You can map all selected options and return them as a delimited string (,) 1)您可以映射所有选定的选项,并将它们作为定界字符串 (,)

2) Now split the string into an array . 2)现在字符串拆分为一个数组

3) Remove items that are moved from the From select element to the To one. 3) 删除被从移动选择元件到要将一个项目。

4) Finally loop through the array appending the options to the second select element. 4)最后遍历数组,将选项附加到第二个select元素。


Multi Select Example below 下面的多重选择示例

 //move items from fabricBox to fabricBox_to $("#move").on("click", function(){ MoveToandFrom("#fabricBox", "#fabricBox_to"); }); //You can even move the options back if you need to $("#moveBack").on("click", function(){ MoveToandFrom("#fabricBox_to", "#fabricBox"); }); //reusable function that moves selected indexes back and forth function MoveToandFrom(fromSelectElement, toSelectElement) { //get list of selected options as delimited string var selected = $(fromSelectElement + " option:selected").map(function(){ return this.value }).get().join(", "); //if no option is selected in either select box if( $.contains(selected, "") ){ alert("You have to select an option!"); return false; } //split the selected options into an array var stringArray = selected.split(','); //remove selected items from selected select box to second select box $(fromSelectElement + " option:selected").remove(); //loop through array and append the selected items to Fabric_to select box $.each( stringArray, function( key, value ) { $(toSelectElement).append("<option>" + value + "</option>"); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <label for="fabricBox">From</label> <select name="from[]" id="fabricBox" class="form-control" size="8" multiple="multiple" style="width:100%"> <option>Fabric 1</option> <option>Fabric 2</option> <option>Fabric 3</option> <option>Fabric 4</option> <option>Fabric 5</option> <option>Fabric 6</option> </select> <button id="move">Move Selected</button> </div> <br/> <div> <label for="fabricBox_to">To</label> <select name="to[]" id="fabricBox_to" class="form-control" size="8" multiple="multiple" style="width:100%" required> </select> <button id="moveBack">Move Selected back</button> </div> 

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

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