本文介绍了jQuery-select2 - 按选择的顺序获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含多个多选项的表单。我使用Select2库。

I'm developing a form with a number of multiselects. I use Select2 library for that.

 $(".chosen-select").select2({
            allow_single_deselect: true,
            width: "150px"
        });
......
<select id="ship2" class="chosen-select" data-placeholder="Select vessel">
<option></option>
<option value="value1">title1</option>
<option value="value2">title2</option>
</select>

问题在于,当我尝试获取所选值时,它们将按照指定的顺序返回< select> 元素,而不是他们被选中的顺序:

The problem is that when I try to get selected values they are returned in the order they are specified in <select> element, not the order they have been selected:

var selectedDayPorts = $('#ship2');
var dayPorts = selectedDayPorts.select2("val");

有没有办法让它们按正确顺序排列?

Is there a way to get them in correct order?

推荐答案

我没有找到如何让select2以正确的顺序存储值的方法,所以我最终为每个select2-field创建隐藏的输入字段并更新它因此我添加/删除新值

I didn't find a way how to make select2 to store values in the correct order, so I ended up creating hidden input fields for each select2-field and update it accordingly as I add/remove new values

这篇关于jQuery-select2 - 按选择的顺序获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 13:01