首页 > 解决方案 > 如何限制选择不同字段的多项选择?

问题描述

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

这是我页面上的 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>

标签: javascriptjqueryformsjquery-chosen

解决方案


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

$('#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%'
 });

推荐阅读