如何在javascript中更改select标签选项?

时间:2014-03-02 03:43:31

标签: javascript html

以下html代码有2个不同的选择标签,具有相同的选项。如果用户更改其中一个,我还希望其他选择标记中的值也会更改。我有一个javascript函数,但它不起作用?

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>

1 个答案:

答案 0 :(得分:2)

不同的ID:

<select name="select-choice-direction-user" id="select-choice-direction-user"
                                                                  No 'n'----^

document.getElementById("select-choice-direction-usern").selectedIndex =  
                                                     ^--- Has an 'n'