jQuery 引导程序多选 - 单击一次即可取消选择/取消选中所有选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22686470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bootstrap multiselect - De-select / Uncheck all options in a single click
提问by 2dar
I'm using Bootstrap multiselectand I would like to add an option to uncheck all selected options. Please provide me with a solution for the same. Thanks for any help :)
我正在使用Bootstrap 多选,我想添加一个选项来取消选中所有选定的选项。请为我提供相同的解决方案。谢谢你的帮助 :)
采纳答案by Chetan Gawai
Try this
尝试这个
$("option:selected").prop("selected", false)
回答by Rajesh Omanakuttan
You can use .multiselect('refresh')
method as mentioned in the bootstrap-multiselect
documentation here.
您可以使用此处文档中.multiselect('refresh')
提到的方法。bootstrap-multiselect
So, add an HTML button / icon next to the multi-select select list and then use the below code:
因此,在多选列表旁边添加一个 HTML 按钮/图标,然后使用以下代码:
$("#reset_client").click(function(){
$('option', $('#select_client')).each(function(element) {
$(this).removeAttr('selected').prop('selected', false);
});
$("#select_client").multiselect('refresh');
});
<select name="clients[]" multiple="multiple" id="select_client" style="display: none;">
<option value="multiselect-all"> Select all</option>
<option value="1">Client 1</option>
<option value="2">Client 2</option>
<option value="3">Client 3</option>
<option selected="selected" value="4">Client 4</option>
<option selected="selected" value="5">Client 5</option>
<option value="6">Client 6</option>
<option selected="selected" value="7">Client 7</option>
<option value="8">Client 8</option>
</select>
<input type="button" value="Reset" name="reset_clients" id="reset_client" class="reset_button" title="Clear Selection">
回答by csga5000
You can include a "select all" button, pushing that twice would "unselect all". To do this add this option:
您可以包含一个“全选”按钮,按两次将“取消全选”。为此,请添加此选项:
includeSelectAllOption:true
This will deslect all for a bootstrap multiselect based on a select element.
这将取消所有基于选择元素的引导多选。
function multiselect_deselectAll($el) { $('option', $el).each(function(element) { $el.multiselect('deselect', $(this).val()); }); } $('.multiselect').each(function() { var select = $(this); multiselect_deselectAll(select); });
function multiselect_deselectAll($el) { $('option', $el).each(function(element) { $el.multiselect('deselect', $(this).val()); }); } $('.multiselect').each(function() { var select = $(this); multiselect_deselectAll(select); });
You could add an option by hand that says "Select all" and then when it's pressed call the reset function.
您可以手动添加一个选项,上面写着“全选”,然后在按下时调用重置功能。
See: https://github.com/davidstutz/bootstrap-multiselect/issues/271
参见:https: //github.com/davidstutz/bootstrap-multiselect/issues/271
回答by sushil suthar
Use below code to deselect all select item in multiselect bootstrap dropdown
使用下面的代码取消选择多选引导下拉列表中的所有选择项
$("#ddlMultiSelectId").multiselect('clearSelection');
回答by Thylle
You can do this with the "deselectAll" method.
您可以使用“deselectAll”方法执行此操作。
$("select.multiselect").multiselect("deselectAll", false);
It is important that you tell jQuery to target your "select's" only, otherwise it will fail. This is because the plugin adds something else with the same class name.
重要的是您告诉 jQuery 只针对您的“选择”,否则它将失败。这是因为插件添加了具有相同类名的其他内容。
@L_7337 - This is not a duplicate of the post you link to. This post is about how to deselect all options, if you use Bootstrap Multiselect
@L_7337 - 这不是您链接到的帖子的副本。这篇文章是关于如何取消选择所有选项,如果你使用Bootstrap Multiselect
The other post, is how to deselect, from a normal multiselect option.
另一篇文章是如何从普通的多选选项中取消选择。
Best regards Rasmus
最好的问候拉斯穆斯
回答by Mr Nobody
If you have the below multi-select:
如果您有以下多选:
<div class="form-group">
<label>Choose Skills</label>
<select id="DDL" name="selValue" class="selectpicker form-control" multiple>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div>
Then you can use the below jQuery to deselect all option on a click of a button:
然后,您可以使用下面的 jQuery 单击按钮取消选择所有选项:
$('#DDL').selectpicker("deselectAll", true).selectpicker("refresh");
回答by Ashutosh gupta
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class = "container">
<div class="modal show" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" data-dismiss="modal">×</button>
<nav class="navbar navbar-default">
<div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">Channel(s)</a></li>
<li><a href="#">Collaborator(s)</a></li>
</ul>
</div>
</nav>
</div>
<div class="modal-body">
<form> <div class="form-check row">
<label class="col-sm-2 form-check-label" for="ListeningProject">Listening Project</label>
<div class="col-sm-10">
<input class="form-check-input" id="ListeningProject" value="" type="checkbox" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-check-label" for="AccountName">Select:</label>
<div class="col-sm-10">
<select class="form-control" id="AccountName" multiple="" required="">
<option>A Company</option>
<option>B Copany</option>
<option>C Company</option>
<option>D Company</option>
</select>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$("#ListeningProject").change(function(){ //"select all" change
var status = this.checked; // "select all" checked status
$('#AccountName option').prop('selected', status);
});
</script>
</body>
</html>
回答by Yevgeniy Afanasyev
The issue was fixed in a gitlab fork here
该问题已在此处的 gitlab fork 中修复
it says:
它说:
Fixed trigger onSelectAll when deselecting Select All
修复了取消全选时触发 onSelectAll
Alternatively we can wait while issue #752will be resolved. It was raised only one month ago.
或者,我们可以等待问题 #752将得到解决。它仅在一个月前提出。
回答by vohrahul
Try this:
尝试这个:
$("#select_id option:selected").removeAttr("selected");
回答by Suresh Kamrushi
You can do like this, it works for me:
你可以这样做,它对我有用:
$('#dropdownID').val([]).multiselect('refresh')