Javascript 如何限制选择不同领域的多选?

Javascript 如何限制选择不同领域的多选?,javascript,jquery,forms,jquery-chosen,Javascript,Jquery,Forms,Jquery Chosen,我有3个不同的多选择字段,我想为这些字段设置不同的限制。如何为不同的类设置max_selected_选项 这是我页面上的js代码: <script type="text/javascript"> $('.form-control-chosen').chosen({ max_selected_options: 5, allow_single_deselect: true, width: '100%' }); $('

我有3个不同的多选择字段,我想为这些字段设置不同的限制。如何为不同的类设置max_selected_选项

这是我页面上的js代码:

<script type="text/javascript">
    $('.form-control-chosen').chosen({
        max_selected_options: 5,
        allow_single_deselect: true,
        width: '100%'
    });
    $('.form-control-chosen-required').chosen({
        allow_single_deselect: false,
        width: '100%'
    });
    $('.form-control-chosen-search-threshold-100').chosen({
        allow_single_deselect: true,
        disable_search_threshold: 100,
        width: '100%'
    });

    $(function () {
        $('[title="clickable_optgroup"]').addClass('chosen-container-optgroup-clickable');
    });
    $(document).on('click', '[title="clickable_optgroup"] .group-result', function () {
        var unselected = $(this).nextUntil('.group-result').not('.result-selected');
        if (unselected.length) {
            unselected.trigger('mouseup');
        } else {
            $(this).nextUntil('.group-result').each(function () {
                $('a.search-choice-close[data-option-array-index="' + $(this).data('option-array-index') + '"]').trigger('click');
            });
        }
    });
</script>

$('.form控件已选定')。已选定({
最大选定选项数:5,
允许单次取消选择:true,
宽度:“100%”
});
$('.form控件已选择,必需')。已选择({
允许单次取消选择:false,
宽度:“100%”
});
$('.form-control-selected-search-threshold-100')。已选择({
允许单次取消选择:true,
禁用搜索阈值:100,
宽度:“100%”
});
$(函数(){
$('[title=“clickable_optgroup”]')。addClass('selected-container-optgroup-clickable');
});
$(文档).on('click','[title=“clickable_optgroup”].group result',函数(){
var unselected=$(this).nextUntil('.group result')。not('.result selected');
如果(未选择的长度){
未选中。触发器('mouseup');
}否则{
$(this).nextUntil('.group result')。each(函数(){
$('a.search-choice-close[data option array index=“”+$(this).data('option-array-index')+“])。触发器('click');
});
}
});

您可以使用Id而不是类作为选择器

$('#multiple-select-field1').chosen({ /// use Id of the element
    max_selected_options: 5,    /// change this value 
    allow_single_deselect: true,
    width: '100%'
}); 

 $('#multiple-select-field2').chosen({ /// use Id of the element
    max_selected_options: 6, /// change this value 
    allow_single_deselect: true,
    width: '100%'
  });

  $('#multiple-select-field3').chosen({ /// use Id of the element
    max_selected_options: 7, /// change this value 
    allow_single_deselect: true,
    width: '100%'
 });

您可以使用Id而不是类作为选择器

$('#multiple-select-field1').chosen({ /// use Id of the element
    max_selected_options: 5,    /// change this value 
    allow_single_deselect: true,
    width: '100%'
}); 

 $('#multiple-select-field2').chosen({ /// use Id of the element
    max_selected_options: 6, /// change this value 
    allow_single_deselect: true,
    width: '100%'
  });

  $('#multiple-select-field3').chosen({ /// use Id of the element
    max_selected_options: 7, /// change this value 
    allow_single_deselect: true,
    width: '100%'
 });