如何在javascript中更改select标记选项?
下面的html代码有两个具有相同选项的不同select标记。如果用户更改其中一个,我还希望另一个select标记中的值也更改。我有一个javascript函数可以实现这一点,但它不工作如何在javascript中更改select标记选项?,javascript,html,Javascript,Html,下面的html代码有两个具有相同选项的不同select标记。如果用户更改其中一个,我还希望另一个select标记中的值也更改。我有一个javascript函数可以实现这一点,但它不工作 function change(str) { document.getElementById("select-choice-direction-user") .selectedIndex = document.getElementById("select-choice-direction"
function change(str) {
document.getElementById("select-choice-direction-user")
.selectedIndex = document.getElementById("select-choice-direction")
.selectedIndex;
}
<select name="select-choice-direction" id="select-choice-direction" data-native-menu="false" onchange="change(this);">
<option value="1">1 Blueridge-McKee GoLine</option>
<option value="2">2 Bluejay-Huntingdon GoLine</option>
<option value="3">3 Clearbrook-UFV GoLine</option>
<option value="4">4 Saddle</option>
<option value="5">5 Hospital</option>
<option value="6">6 Gladwin-Peardonville</option>
</select>
<select name="select-choice-direction-user" id="select-choice-direction-user" data-native-menu="false" onchange="change(this);">
<option value="1">1 Blueridge-McKee GoLine</option>
<option value="2">2 Bluejay-Huntingdon GoLine</option>
<option value="3">3 Clearbrook-UFV GoLine</option>
<option value="4">4 Saddle</option>
<option value="5">5 Hospital</option>
<option value="6">6 Gladwin-Peardonville</option>
</select>
功能更改(str){
document.getElementById(“选择选择方向用户”)
.selectedIndex=document.getElementById(“选择选择方向”)
.选择的索引;
}
1蓝脊麦基戈林酒店
2蓝鸦亨廷顿戈林酒店
3 Clearbrook UFV GoLine
4鞍
5医院
格拉德温·皮尔登维尔6号
1蓝脊麦基戈林酒店
2蓝鸦亨廷顿戈林酒店
3 Clearbrook UFV GoLine
4鞍
5医院
格拉德温·皮尔登维尔6号
不同的ID:
<select name="select-choice-direction-user" id="select-choice-direction-user"
No 'n'----^
document.getElementById("select-choice-direction-usern").selectedIndex =
^--- Has an 'n'
考虑到刚才所做的编辑,这不能出现在实际代码中。@isherwood是对的,它没有出现在实际代码中