如何在单击事件中清除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>
答案 0 :(得分:4)
您可以通过遍历该选项来清除,并取消选择它们,例如:
$(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>
答案 1 :(得分:0)
供将来参考: 使用具有更好重载功能的fSelect的fork,执行以下操作可以重置值和所选项目:
(function($) {
$(function() {
$('.my-select-box').fSelect();
$('#ClearSelect').click(function(){
$("#fselectMulti option").prop("selected", false);
$('.my-select-box').fSelect('reload');
})
});
})(jQuery);