13 回答
Simply find all the selected <option> tags within your <select> and remove the selected attribute:
$("#my_select option:selected").removeAttr("selected");
As of jQuery 1.6, you should use .prop instead of removing the attribute:
$("#my_select option:selected").prop("selected", false);
为了清除所有选择,我像这样使用它,它对我来说工作正常。这是脚本:
$("#ddlMultiselect").multiselect("clearSelection");
$("#ddlMultiselect").multiselect( 'refresh' );
这是清除多选或列表框的最佳方法:
$("#drp_assign_list option[value]").remove();
删除所有选择的最短和最快的方法是在 jQuery .val() 函数中传递空字符串:
$("#my_select").val('')
在 JQuery 中:
$("#id option:selected").prop("selected", false);
$("#id").multiselect('refresh');
$("#ddlMultiselect").val('').trigger("change");
对于多选下拉菜单,它将清除显示的文本。
jQuery's :selected selector is probably what you are looking for.
在 JavaScript 中,
您可以使用多选下拉列表的所有选项列表,该列表将是一个数组。然后循环遍历它以使每个对象中的选定属性为 false。
for(var i=0;i<list.length;i++)
{
if(list[i].Selected==true)
{
list[i].Selected=false;
}
}
$('.clearAll').on('click', function() {
$('.dropdown').val([]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="dropdown" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<button class='clearAll'>Clear All Selection</button>
您可以传递一个空数组来取消选择所有选择。这是一个例子。从文档
val()允许您传递元素值的数组。这在处理包含 、 和 s 等元素的 jQuery 对象时很有用。在这种情况下,将检查或选择具有与数组元素之一匹配的值的输入和选项, 而具有与数组元素之一不匹配的值的输入和选项将被取消选中或取消选中,具体取决于在类型上。
$('.clearAll').on('click', function() {
$('.dropdown').val([]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="dropdown" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<button class='clearAll'>Clear All Selection</button>
我尝试了很多解决方案并想出了这个。
仅使用此选项清除下拉列表的选项和选择
$("#my-multi option:selected").prop("selected", false);
$("#my-multi option").remove();
但是界面没有更新。所以你必须这样做
$('#my-multi').multiselect('rebuild');
因此,最终代码是
$("#my-multi option:selected").prop("selected", false);
$("#my-multi option").remove();
$('#my-multi').multiselect('rebuild');
欢迎提出建议和改进。
$('.selectpicker').selectpicker('deselectAll');
https://developer.snapappointments.com/bootstrap-select/methods/
假设您正在使用 David Stutz 的 Bootstrap 多选下拉菜单
$('#selectId').multiselect('deselectAll', false);
但由于某种原因,它在初始化方法中不起作用