如何根据选定选项的值更改选定选项?

时间:2019-01-14 09:50:11

标签: javascript html drop-down-menu

我下面有select标签:

<select name="selID" id="selID" data-mini="true"">
    <option value="1" selected="selected">Tithes</option>
    <option value="2">Offering</option>
    <option value="3">Designated</option>
    <option class="selOptAddMore" value="0">Add More...</option>
</select>

当我选择“添加更多...”以外的选项之一时,我想更改值。

对不起,我是新手。预先感谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

HTML:

<body>
    Result: <p id="result"></p>

    <select name="selID" id="selID" data-mini="true" onchange="test()">
        <option value="1" selected="selected">Tithes</option>
        <option value="2">Offering</option>
        <option value="3">Designated</option>
        <option class="selOptAddMore" value="0">Add More...</option>
    </select>

    <script src="test.js"></script>
</body>


test.js:

    function test() {
        let listVal = document.getElementById("selID").value;
        if (listVal != "0") {
            document.getElementById("result").innerHTML = listVal;
        }
        else {
            listVal = 1; // this is value for "Tithes"
            document.getElementById("selID").value = listVal;
            document.getElementById("result").innerHTML = listVal;
       }
    }