JQuery 使用 .prop('selectedIndex', #) 设置 select 无效

JQuery set select using .prop('selectedIndex', #) not working

我正在使用以下行从 select 菜单中设置一个选项:

$('select').prop('selectedIndex', 2)

以以下网站为例: http://shoebaloo.nl/michael-by-michael-kors-chelsea-skate-leo-bruin-dessin-285000097-women-sneakers.html

我确实看到 select 选项转到了第二个选项,但它不会产生与实际单击第二个选项相同的效果。

有人可以帮助我让页面真正识别出我 "clicking" 的选项吗?

您需要 select 带有 select 或

的选项
$('select option:eq(2)').prop('selected', true)

如果您希望手动触发点击或更改事件,您可以使用 .trigger('click').trigger('change') 方法。

http://jsfiddle.net/j5v6tp7t/4/

您所要做的就是使用.val()设置值。

例如:

$('select').val('2')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select>
    <option value="1">a</option>
    <option value="2">b</option>
    <option value="3">c</option>
    <option value="4">d</option>
</select>

你的表达是正确的,但正确的语法是:

$('select').get(0).selectedIndex = 2;

http://jsfiddle.net/f4s762cq/