使用val()和prop在多选中取消选择值 [英] Unselecting values in multiselect using val() and prop
本文介绍了使用val()和prop在多选中取消选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看看这个小提琴: http://jsfiddle.net/imparator/XPGa8/1/
首先,我选择选择列表中的值Volvo:
First, I am selecting the value "Volvo" of the select list:
$("select").val('volvo');
稍后,当我尝试取消选择它时:
Later, when I try to unselect it like this:
$("select").val('volvo').prop("selected", false);
从小提琴中可以看到,沃尔沃值仍保持选中状态。
The value volvo still stays selected as you can see from the fiddle.
如何使用jquery取消选择multiselect中的值?
How can I unselect a value in multiselect using jquery?
推荐答案
使用 $(select option [value ='volvo'])。prop(选择,false);
jsFiddle example
此外,您应将选项标记为选中,如下所示:
Also, you should mark an option as selected like this:
$("select option[value='volvo']").prop("selected", true);
$("select option[value='audi']").prop("selected", true);
这篇关于使用val()和prop在多选中取消选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文