我知道select2没有“readonly”功能。请查看这里。
我该怎么做呢?
任何帮助都将不胜感激。
谢谢。
注意:我不能使用disabled。如果我使用disabled,我将无法获取所选值的列表。
$("select").select2("readonly", true);
您可以放置自己的CSS,例如:
.select2-container.select2-container-disabled .select2-choice {
background-color: #ddd;
border-color: #a8a8a8;
}
正如问题所述:如何使select2只读?,并且正如用户指出的那样:select2没有“readonly”功能。根据select2团队开发人员的说法:阅读此文。
我想补充几点:
$(element).select2({ disabled: true });
仅适用于V4之前的版本。实际上,标记为正确的答案是指V3。话虽如此,我想分享一个简单的建议:
$(element).select2('destroy').attr("readonly", true)
$(element).select2()
提示:
$(element).select2('destroy').attr("readonly", true).css({'-moz-appearance': 'none','-webkit-appearance': 'none'});
$('.js-example-basic-single').select2({
disabled: true
});
试试这个:
$('select option:selected').attr('disabled','disabled');
编辑:
对于使用 Select 2 版本 4+ 的用户,现在的做法应该是:
$("select").prop("disabled", true); // instead of $("select").enable(false);
在澄清问题之后,这是正确的做法:
$('select').select2().enable(false);
$('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>
根据新版本的select2,您无法将其设置为只读。
请点击链接获取CSS hack,它能够完美解决问题。
试试这个:
$(".example_element").css("pointer-events","none");
如果您只想让选择变为只读模式,应该使用
$('select').select2({
readonly: true
});
您可以使用
$("#id_example").select2({readonly:true});
禁用在POST操作中发送数据。
$('#element_id').select2().readonly(true);
这可以定义您的元素为只读,同时仍然发送您的POST数据