经过几次测试,试图阻止
扩大
/
开放
在Select2项中,我找到了在每个本地select标记上应用一个侦听器的方法,该标记具有Select2属性id。。。并在打开事件时检测本机标记是否为
readonly
.
$('select[data-select2-id]').on('select2:opening', function (e) {
if( $(this).attr('readonly') == 'readonly') { // Check if select tag is readonly.
console.log( 'readonly, can't be open !' );
e.preventDefault();
$(this).select2('close');
return false;
}else{
console.log( 'expandable/selectable' );
}
});
要在Select2上进行更多自定义,我们可以添加一些CSS。。。
select[readonly] ~ .select2.select2-container .selection [role="combobox"] {
background: repeating-linear-gradient(
45deg
, #b4d2e4, #b4d2e4 10px, rgba(255, 255, 255, 0.15) 10px, rgba(255, 255, 255, 0.15) 20px) !important;
box-shadow: inset 0 0 0px 1px rgba
jQuery(document).ready(function($) {
// Implement Select2
$( 'select' ).select2({
placeholder: "Saisissez un mot pour aide à saisie",
tags: true, // allow create new
width: '100%'
});
// Just extra button to swap readonly
$('#button_demo_2').off().on( 'click',function(e){
console.log( $('#select_demo_2').attr('readonly') );
if( typeof( $('#select_demo_2').attr('readonly') ) == 'undefined' ){
$('#select_demo_2').attr('readonly','readonly');
}else{
$('#select_demo_2').removeAttr('readonly');
}
} );
// Demo code...
$('select[data-select2-id]').on('select2:opening', function (e) {
if( $(this).attr('readonly') == 'readonly') {
console.log( 'can not open : readonly' );
e.preventDefault();
$(this).select2('close');
return false;
}else{
console.log( 'can be open : free' );
}
});
});
*{
margin : 0;
padding : 0;
}
body {
height: 100vh;
background-color: #215a82;
font-family: 'Roboto',sans-serif;
background: linear-gradient(180deg,#215a82 0%,#152135 100%) no-repeat;
display: -webkit-box !important;
display: -ms-flexbox !important;
display: flex !important;
-webkit-box-align: center !important;
-ms-flex-align: center !important;
align-items: center !important;
-ms-flex-pack: distribute !important;
justify-content: space-around !important;
}
.container {
display: -webkit-box !important;
display: -ms-flexbox !important;
display: flex !important;
}
div[role="box"]{
padding:1rem;
margin:2rem
display: block;
}
pre {
line-height: 1rem;
height: 1.5rem;
color: white;
}
select[readonly] ~ .select2.select2-container .selection [role="combobox"] {
background: repeating-linear-gradient(45deg, #dadada, #dadada 10px, rgba(255, 255, 255, 0.66) 10px, rgba(255, 255, 255, 0.66) 20px) !important;
box-shadow: inset 0 0 0px 1px #77859133;
}
input{
display: block;
padding: 0.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<main class="container">
<div role="box">
<pre></pre>
<select class="form-control inputFocusable" id="select_base" name="select_base" tabindex="-1" aria-hidden="true">
<option value="A">Item A</option>
<option value="B">Item B</option>
<option value="C">Item C</option>
</select>
</div>
<div role="box">
<pre>readonly</pre>
<select class="form-control inputFocusable" id="select_demo_1" name="select_demo_1" tabindex="-1" aria-hidden="true" readonly>
<option value="A">Item A</option>
<option value="B">Item B</option>
<option value="C">Item C</option>
</select>
</div>
<div role="box">
<pre>readonly="readonly"</pre>
<select class="form-control inputFocusable" id="select_demo_2" name="select_demo_2" tabindex="-1" aria-hidden="true" readonly="readonly">
<option value="A">Item A</option>
<option value="B">Item B</option>
<option value="C">Item C</option>
</select>
</div>
</main>
<div role="box">
<pre>readonly ?</pre>
<input id="button_demo_2" type="button" value="Fix / Remove">
</div>