fSelect Clear Selection

后端 未结 2 832
日久生厌
日久生厌 2021-01-03 13:38

How to clear fSelect multi-select drop-down in click event, Here is an example By Clicking on Clear button It should remove all Selected value.

相关标签:
2条回答
  • 2021-01-03 14:23

    You could clear by looping through the option and unselect them like :

    $(function() {
      $('.my-select-box').fSelect();
    
      $('#ClearSelect').click(function() {
        $('#fselectMulti option:selected').removeAttr('selected');
    
        $('.my-select-box').prev(".fs-dropdown").find(".fs-options .fs-option").each(function() {
          $(this).removeClass('selected', false);
        });
    
        console.log($('#fselectMulti').val());
        $('.fs-label').html('Select some options');
      })
      $('#CheckSelect').click(function(){
        console.log($('#fselectMulti').val());
      })
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://facetwp.com/wp-content/plugins/facetwp/assets/vendor/fSelect/fSelect.css" rel="stylesheet" />
    <script src="https://facetwp.com/wp-content/plugins/facetwp/assets/vendor/fSelect/fSelect.js"></script>
    <select id="fselectMulti" class="my-select-box" multiple="multiple">
      <optgroup label="Northeast">
        <option selected value="me">Maine</option>
        <option selected value="ny">New York</option>
        <option value="nj">New Jersey</option>
        <option value="vt">Vermont</option>
      </optgroup>
      <optgroup label="Southwest">
        <option value="az">Arizona</option>
        <option selected value="nm">New Mexico</option>
        <option value="ca">California</option>
        <option value="nv">Nevada</option>
      </optgroup>
    </select>
    
    <button id="ClearSelect">Clear</button>
    <button id="CheckSelect">Check Array</button>

    0 讨论(0)
  • 2021-01-03 14:26

    For future references: Using a fork of fSelect which has a better reload function, doing the following can reset the values and the selected items:

    (function($) {
        $(function() {
            $('.my-select-box').fSelect();
            $('#ClearSelect').click(function(){
                $("#fselectMulti option").prop("selected", false);
                $('.my-select-box').fSelect('reload');
            })
        });
    
    })(jQuery);
    
    0 讨论(0)
提交回复
热议问题