javascript - 如何限制选择不同字段的多项选择?
问题描述
我有 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>
解决方案
您可以使用 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%'
});
推荐阅读
- ruby-on-rails - 生产中没有选择风格的导轨
- wordpress - Woocommerce 产品可由其作者针对特定用户角色进行编辑
- .net - 调用 Siemens WinAC ODK Open() 时出现错误代码 2
- vb.net - vb.net 中的机器密钥生成
- c# - C# binarywriter 字符串的开始和结束
- python - 如果您分块加载 csv,pandas dropna() 将不起作用
- javascript - 有条件地创建对象
- python - 使用 boto3 更改 s3 对象 CacheControl
- javascript - 如何在 echartjs 中为树形图添加更多填充?
- sockets - 如何使用 Dart 套接字 onError 作为内联函数?