twitter-bootstrap 如何在通过 jQuery 使用另一个 Bootstrap Multiselect 时重置一个 Bootstrap Multiselect
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38031459/
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
How to reset one Bootstrap Multiselect when another Bootstrap Multiselect is used via jQuery
提问by Terry
I am using the Bootstrap-Multiselect (http://davidstutz.github.io/bootstrap-multiselect/)
我正在使用 Bootstrap-Multiselect ( http://davidstutz.github.io/bootstrap-multiselect/)
I have two dropdowns, when an item is selected in one dropdown I need to reset the other dropdown. I have been attempting to do this using the onchange jQuery but without success.
我有两个下拉列表,当在一个下拉列表中选择一个项目时,我需要重置另一个下拉列表。我一直在尝试使用 onchange jQuery 来做到这一点,但没有成功。
If an option is selected from dropdown1, then deselect/reset everything in dropdown2.
如果从下拉列表 1 中选择了一个选项,则取消选择/重置下拉列表 2 中的所有内容。
<form>
<select id="dropdown1" name="dropdown1" multiple="multiple">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
If an option is selected from dropdown2, then deselect/reset everything in dropdown1.
如果从 dropdown2 中选择了一个选项,则取消选择/重置 dropdown1 中的所有内容。
<select id="dropdown2" name="dropdown2" multiple="multiple">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
My failed jQuery
我失败的 jQuery
$("#dropdown1").change(function () {
$('#dropdown2').multiselect('refresh');
});
$("#dropdown2").change(function () {
$('#dropdown1').multiselect('refresh');
});
How should this be accomplished?
这应该如何实现?
回答by d3v_1
I suspect you need to reset the select element and then do the refresh, like so:
我怀疑您需要重置 select 元素,然后进行刷新,如下所示:
$("#dropdown1").change(function () {
$('#dropdown2 option:selected').each(function() {
$(this).prop('selected', false);
})
$('#dropdown2').multiselect('refresh');
});
$("#dropdown2").change(function () {
$('#dropdown1 option:selected').each(function() {
$(this).prop('selected', false);
})
$('#dropdown1').multiselect('refresh');
});
That code is borrowed from the reset button code on the Bootstrap Multiselect site. See the further examples
该代码是从 Bootstrap Multiselect 站点上的重置按钮代码中借来的。查看更多示例
回答by Pooja Mistry
Another way of deselecting all options. (Because multiselect('refresh') did not work for me.)
另一种取消选择所有选项的方法。(因为 multiselect('refresh') 对我不起作用。)
$('#ID').multiselect('deselectAll', false);
$('#ID').multiselect('updateButtonText');
回答by UWU_SANDUN
This is working for me.
这对我有用。
$('#ID').val('').multiselect('refresh');

