JSFiddle:http://jsfiddle.net/xpvt214o/776660/
打开选择框后,如果您尝试在选择框的级别上在以外的任何地方单击,它不会关闭。但是,如果在“当前选择”行的级别上单击外部,则它确实会关闭。
当执行任何外部单击(包括选择框的级别)时,如何强制关闭Select2选择框?
P.S。我正在使用closeOnSelect: false
选项在选择过程中始终使选择框保持打开状态-但在外部单击时仍应关闭它,
$('#dropdown').select2({
closeOnSelect: false
});
另一个类似的问题-closing select2 on click away when closeonselect is false
答案 0 :(得分:2)
不知道是什么原因导致的问题,但是将其添加到CSS可以解决问题
html,
body{
height: 100%;
}
更新的代码
/* Scroll CurrSel SPAN to bottom on every Select2 Select event */
$('#dropdown').on("select2:selecting", function(e) {
var currselspan = $('#dropdown').next().find('.select2-selection--multiple').first();
console.log('scrollTop = ' + $(currselspan).scrollTop() + ' scrollHeight = ' + $(currselspan).prop('scrollHeight'));
$(currselspan).scrollTop($(currselspan).prop('scrollHeight'));
});
$('#dropdown').select2({
closeOnSelect: false,
});
.select2-selection--multiple {
height: 2rem;
max-height: 2rem;
overflow: auto;
}
html,
body {
height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.full.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" rel="stylesheet" />
<div style="width: 50%">
<select id="dropdown" style="width: 100%" multiple>
<option>TEST ELEMENT 1</option>
<option>TEST ELEMENT 2</option>
<option>TEST ELEMENT 3</option>
<option>TEST ELEMENT 4</option>
<option>TEST ELEMENT 5</option>
<option>TEST ELEMENT 6</option>
<option>TEST ELEMENT 7</option>
<option>TEST ELEMENT 8</option>
<option>TEST ELEMENT 9</option>
<option>TEST ELEMENT 10</option>
<option>TEST ELEMENT 11</option>
<option>TEST ELEMENT 12</option>
<option>TEST ELEMENT 13</option>
</select>
</div>