代码之家  ›  专栏  ›  技术社区  ›  Rajesh Samui

如何使select2只读?

  •  5
  • Rajesh Samui  · 技术社区  · 6 年前

    我知道select2没有“只读”功能。请检查 here . 我如何做到这一点? 任何帮助都将不胜感激。 谢谢 注意:我不能使用disabled。如果使用disabled,则不会获取所选值的列表。

    5 回复  |  直到 6 年前
        1
  •  7
  •   Manisha Bano    6 年前

    我想这个问题已经解决了,或者可以在更高版本中使用,因为这对我有用。

    请在此处查看示例以使select2只读: Select 2

    在js代码中:

    $("select").select2("readonly", true);
    

    您可以放置自己的CSS,如:

    .select2-container.select2-container-disabled .select2-choice {
      background-color: #ddd;
      border-color: #a8a8a8;
    }
    
        2
  •  4
  •   Victor Matuk    6 年前
    $('.js-example-basic-single').select2({
      disabled: true
    });
    

    在为我解析页面加载后呈现禁用的属性。

    提醒,具有禁用属性的字段不会在表单上发送

        3
  •  2
  •   Rashed Hasan Vijayaragavan    5 年前

    尝试以下操作:

    $('select option:selected').attr('disabled','disabled');
    

    编辑 :

    对于使用Select 2版本4+的用户,新的方法应该是:

    $("select").prop("disabled", true); // instead of $("select").enable(false);
    

    澄清问题后,这是正确的方法:

    $('select').select2().enable(false);
    
        4
  •  1
  •   EsseBé    3 年前

    经过几次测试,试图阻止 扩大 / 开放 在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>
        5
  •  0
  •   absolutkarlos    4 年前

    正如问题所说: 如何使select2只读? ,正如用户所指出的: select2不存在函数“readonly” . 根据select2团队开发人员: read this.

    我只想补充几点:

    • 禁用与只读不同!小心。
    • $(element).select2({ disabled: true }); 仅适用于V4之前的版本。事实上,标记为正确的答案指的是V3。

    话虽如此,我想分享一个简单的建议:

    • 销毁元素并使其只读。 $(element).select2('destroy').attr("readonly", true)
    • 如果你还需要它,你随时可以再打电话给他。 $(element).select2()

    提示:

    • 如果希望它看起来像前面的元素,只需删除默认的css样式: $(element).select2('destroy').attr("readonly", true).css({'-moz-appearance': 'none','-webkit-appearance': 'none'});