Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

javascript - Hide select2 multi search box

enter image description hereI am trying to make a select2 search box visible if the values in a HTML drodown match a value. Following is my code snippet. Somehow, the Select2 dropdown is always shown. Any help is appreciated...

I have added the CSS based on the suggestion on select2 github repo https://github.com/select2/select2/issues/861

<html>
<style type="text/css">
.other {
    display: none !important;
}
</style>

<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.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>

          <select class="one" name="one" id="one">
            <option value="1">1</option>
            <option value="2">3</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            
         </select>
         <br>
  
<br>


    <select class="other" name="other" multiple="multiple">
  <option value="AL">Sample1</option>
  <option value="WY">Sample2</option>
    </select>
<script>
   // In your Javascript (external .js resource or <script> tag)
  

  $("#one").change(function() {

  $('#value').change(function()
  {
    if($('#value').val() == '1')
    {
        $('.other').hide();
    } else 
    {
        $('.other').show();
    }
}
)});
  $(document).ready(function() {
      $(".other").select2({
          tags: true
      });
  });
  
    
    </script>
  </html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Can you do something like this ? Not sure about your usercase. But I will suggest to destroy the select and then hide. And later again create the instance and show.

$("#one").change(function() {

    if($(this).val() == '3')
    {
        $('.other').select2('destroy');
      $('.other').hide();
    } else {
        $(".other").select2({
          tags: true
      });
        $('.other').show();
    }
});
  $(document).ready(function() {
      $(".other").select2({
          tags: true
      });
  });
  
<html>
<style type="text/css">
.other {
    display: none !important;
}
</style>

<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.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>

          <select class="one" name="one" id="one">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            
         </select>
         <br>
  
<br>


    <select class="other" name="other" multiple="multiple" style="width: 100px;">
  <option value="AL">Sample1</option>
  <option value="WY">Sample2</option>
    </select>

  </html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...