将select2限制为最多6个选项

Restrict select2 to a max of 6 choices

本文关键字:6个 选项 select2      更新时间:2023-09-26

我在Ajax中使用非多重下拉select2。我需要将选定的选项限制为6个。

我尝试过使用maximumSelectionSize,但这只适用于倍数。

然后我看了看:

.on('select2-selecting', function(e){
// counting the ids but this did not work
      var hasId = ($('[id^=select2-chosen-]').length);
});

我真的很挣扎,所以任何帮助都是非常感激的。

选择时,您可以返回选项值,并将其长度限制为6个字符。

.on('select2-selecting', function(e){
     var returnValue = $(this).val().substr(0,6); //remove anything after 6 charaters.
    console.info(returnValue);
});