我有下面的代码,我试图改变两个选择输入的默认值,我期望假为第一个对话和你好为第二个对话,但有些不对。使用dom将选择标记选项更改为默认值?
这是代码行应该做出改变
var vSelect = document.getElementById('select_1');
vSelect.options[0].defaultSelected = 1;
vSelect = document.getElementById('select_2');
vSelect.options[1].defaultSelected = 0;
用于代表国家各select
标签内,或者是它们的全球指数在页面内的所有option
标签options[index]
?
我真正想要做的就是将一个特定的select
标签设置为默认选项,而不必首先知道选项索引,让我知道为什么这是不可能的,如果是这样的话。
<html>
<head>
</head>
<body onload = "default_settings()">
<style type = "text/css">
body
{
background-color: white;
}
#select_1, #menu_select_label_id_1, #select_2, #menu_select_label_id_2
{
color:#64FFFF;
font-size:14px;
background: rgba(0,0,0,1.0);
text-align: left;
max-width:90px;
padding: 4px 4px;
}
</style>
<script language="JavaScript">
function default_settings()
{
var vSelect = document.getElementById('select_1');
vSelect.options[0].defaultSelected = 1;
vSelect = document.getElementById('select_2');
vSelect.options[1].defaultSelected = 0;
}
</script>
<select id = "select_1">
<option value = "0">true</option>
<option value = "1">false</option>
</select>
<label id = "menu_select_label_id_1" for = "select_1">
boolean
</label>
<br>
<select id = "select_2">
<option value = "0">hello</option>
<option value = "1">goodbye</option>
</select>
<label id = "menu_select_label_id_2" for = "select2">
greeting
</label>
</body>
</html>
所以每个select选项标签的索引只适用于true或false,每个index都表示不同的value,我将它们设置为true或false。 – pandoragami
@ user2555139:你让这种方式比现在更复杂。每个选择都有一个选项列表。这些选项是按顺序的。每个选项的索引是它在列表中的基于0的位置:第一个选项是索引“0”,第二个选项是索引“1”等。每个选项对象都有属性,如“value”,“text” '','selected'标志和'defaultSelected'标志。 'defaultSelected'标志是选项对象的属性,而不是选择框,它不是索引。这是一面旗帜。默认选项为“true”,其他选项为“false”(在单选框中)。 –
对,我明白了。 – pandoragami