javascript Jquery 选择多选下拉列表中的所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5492542/
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
Jquery select all values in a multiselect dropdown
提问by john
I have a multi-select html dropdown. I want to programatically (via jquery) select all values in the dropdown. How can I do this?
我有一个多选 html 下拉列表。我想以编程方式(通过 jquery)选择下拉列表中的所有值。我怎样才能做到这一点?
回答by Richard Dalton
Where the select has the id test
选择有 id 测试的地方
$('#test option').attr('selected', 'selected');
Or as Ryan mentioned you can use .prop
或者正如瑞安提到的,你可以使用 .prop
$('#test option').prop('selected', true); // or pass false to deselect them
回答by Jon
Simply do $("#dropdown").val([0, 1, 2, 3])
where 0
, 1
, 2
, 3
the values you want to select.
只需执行$("#dropdown").val([0, 1, 2, 3])
where 0
, 1
, 2
,3
您要选择的值即可。