簡體   English   中英

使用數據獲取select2選擇的項目以防止排序

[英]Get select2 selected items using data to prevent sorting

我正在對jQuery select2插件遇到的問題進行一些研究。 在發布表單時,select2對所選項目進行排序(無論您選擇的順序如何)。 我知道這可能不是特定於select2的錯誤,而是標准的html select行為。

現在,如何獲取選定的項目ID,然后將它們放在隱藏輸入中以逗號分隔的列表中,以便我可以尊重選擇項目的順序?

$("#formpacint").submit(function (event) {  
    var data = $('#campoprofesionales').select2('data');  
    $('#hidprofesionales').val(data);  
});

上面的代碼使用其data屬性將選定的項(按照選擇的順序)放入隱藏的輸入中,但是console.log將它們顯示為對象(我猜是text + id)。

我需要:35、14、29(所選項目的ID,因為它們沒有排序就被選中了)

謝謝

找到了解決方案

var selections = (JSON.stringify($("#campoprofesionales").select2('data')));
var obj = $.parseJSON(selections);
$.each(obj, function() {                    
      console.log(this.id);
      // I here set a hidden field with the ids in the order they were selected
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM