2011-03-30 96 views

回答

20

凡选择具有ID测试

$('#test option').attr('selected', 'selected'); 

JSFiddle Example

或者莱恩指出,可以使用.prop

$('#test option').prop('selected', true); // or pass false to deselect them 
+1

+1,这比OP要的要好。 – Jon 2011-03-30 21:26:38

+0

好又简单。 – 2011-03-30 21:31:56

+2

值得注意'attr'和'prop'之间的区别。使用'selected'作为属性只会在引入DOM时设置它的初始状态 - 在该点之后对其状态的任何更改使用'prop'。例如:'.prop('selected',true)'。 – 2014-02-25 16:55:30

0

只要做$("#dropdown").val([0, 1, 2, 3])其中0,1,2,3您想要选择的值。

+0

如果我想将其全部选中(任意金额)。你能告诉我怎么做,因为我还不确定。我是否必须通过循环遍历所有选择来手动构建该数组? – john 2011-03-30 21:22:15

+0

@john:InfernalBadger的回答会更合适:) – Jon 2011-03-30 21:26:22