我想根据传递给函数的值取消选择选择器中的选定选项
要实现这一点,请使用以下代码。我使用onclick
事件取消选中选项a,b ...
$("#select1 option[value='"+myname+"']").prop("selected", false);
function rem(myname) {
$("#select1 option[value='" + myname + "']").prop("selected", false);
}

<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.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>
<div class="container body-content">
Select Fee Types
<select class="selectpicker form-control" id="select1" name="select1" data-width="200px" data-size="4" data-live-search="true" multiple>
<option disabled>Select</option>
<option value="a,b" selected>a</option>
<option value="d,c" selected>c</option>
<option value="f,e">e</option>
</select>
</div>
<button onclick=r em( 'a,b')>Unselect</button>
&#13;
答案 0 :(得分:0)
更新您的功能
$(&#34;#select1&#34;)。您的函数中缺少selectpicker(&#34; refresh&#34;); ,这是更新selectpicker的正确方法
function rem(myname) {
$("#select1 option[value='"+myname+"']").prop("selected", false);
$("#select1").selectpicker("refresh");
}
function rem(myname) {
$("#select1 option[value='"+myname+"']").prop("selected", false);
$("#select1").selectpicker("refresh");
}
&#13;
<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.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>
<div class="container body-content">
Select Fee Types
<select class="selectpicker form-control" id="select1" name="select1" data-width="200px" data-size="4" data-live-search="true" multiple>
<option disabled>Select</option>
<option value="a,b" selected>a</option>
<option value="d,c" selected>c</option>
<option value="f,e">e</option>
</select>
</div>
<button onclick = rem('a,b')>Unselect</button>
&#13;
答案 1 :(得分:0)
好的,你有两个选择。如果您只想取消选择特定值,则可以按照代码段中的说明执行此操作。如果您想要取消选择所有值,您可以这样做:$("#select1").selectpicker('deselectAll');
(source)
我是如何找到答案的?我键入&#34; bootstrap-select方法&#34;在谷歌浏览器,它是第一个结果。请你在问一个问题之前自己做一些研究:)如果你做了,那就没问题;)
function rem(myname) {
$("#select1 option[value='" + myname + "']").prop("selected", false);
$("#select1").selectpicker('refresh');
}
&#13;
<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.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>
<div class="container body-content">
Select Fee Types
<select class="selectpicker form-control" id="select1" name="select1" data-width="200px" data-size="4" data-live-search="true" multiple>
<option disabled>Select</option>
<option value="a,b" selected>a</option>
<option value="d,c" selected>c</option>
<option value="f,e">e</option>
</select>
</div>
<button onclick="rem('a,b')">Unselect</button>
&#13;