编程语言
首页 > 编程语言> > javascript – f选择清除选择

javascript – f选择清除选择

作者:互联网

如何清除fSelect单击事件中的多选下拉列表,下面是一个示例通过单击清除按钮它应该删除所有选定值.

(function($) {
    $(function() {
        $('.my-select-box').fSelect();
        $('#ClearSelect').click(function(){
           $("#fselectMulti option:selected").removeAttr("selected");
        })
    });
    
})(jQuery);
<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>

解决方法:

您可以通过循环选项清除它们,并取消选择它们:

$(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>

标签:javascript,jquery,multi-select,fselect
来源: https://codeday.me/bug/20191006/1858507.html