Jquery 如何使用JavaScript更改select的select选项?

Jquery 如何使用JavaScript更改select的select选项?,jquery,html,select,Jquery,Html,Select,我有一个很简单的问题,但我不知道怎么做。。。 在我的HTML页面中,我有一个简单的select标记: <select id="selection-type"> <option disabled="disabled">Type</option> <option value="0">Tous les types</option> <option value="1">Appartement</o

我有一个很简单的问题,但我不知道怎么做。。。 在我的HTML页面中,我有一个简单的select标记:

<select id="selection-type">
     <option disabled="disabled">Type</option>
     <option value="0">Tous les types</option>
     <option value="1">Appartement</option> 
     <option value="2">Maison</option>
     <option value="3">Terrain</option>
</select>

类型
托斯-莱斯类型
公寓
梅森
地形
我想使用JavaScript动态设置一个特定的值,但我只是设法更改了选定的索引,而不是显示的值。目前,我正在这样做(在我的HTML页面的页脚部分):


$(文档).ready(函数(){
document.getElementById(“选择类型”)。selectedIndex=2;
});

结果是,它改变了我选择的所选索引,但显示的文本不是“更新”:值“Tous les types”保持不变。事实上,我想更改整个选择,例如所选选项,而不仅仅是索引。

看起来您正在使用jQuery。您可以使用jQuery的
val
方法更改选择框,使用要选择的选项的
value

$(document).ready(function() {
    $('#selection-type').val('1');
});
或者你需要用普通的JavaScript来完成吗

编辑:

你用的是什么浏览器?在Chrome 24.0.1312.5中,您所做的
selectedIndex
操作确实对我有用。

您是否尝试过:

document.getElementById("selection-type").value = document.getElementById("selection-type").selectedIndex.value;

最后,我找到了一种方法让它发挥作用。我仍然使用我的第一个方法,但我在$(document)之前执行它。就绪:


document.getElementById(“选择类型”)。selectedIndex=2;
$(文档).ready(函数(){
...
});

我不明白确切的原因。。但这是工作。非常感谢。

如果您使用的是jquery,那么显然您已经在使用javascript了,不需要编辑标记并添加它。(这是给其他编辑器的提示)我尝试了第一种解决方案,但它不起作用。我正在使用firefox进行测试,但我的webiste将是一个移动网站,因此如果它能在Safari上运行,那将非常棒。带有selectedIndex的方法只更改选定的索引,而不更改展开时在“选择”中显示的值。好的,那么您是否要更改特定选项的文本,而不是选择框的选定选项?例如,将文本“Appartement”更改为其他内容?
document.getElementById("selection-type").value = document.getElementById("selection-type").selectedIndex.value;
<script type="text/javascript">
    document.getElementById("selection-type").selectedIndex = 2;
    $(document).ready(function() {
        ...
     });
</script>