如何从 html 下拉列表菜单中仅打印一个选项的选定值

How to print the selected value of only one option from html drop down list menu

你好,我有这个下拉列表菜单,我想制作一个脚本,当我 select 只有最后一个选项时,标签会在 html 中更改。这是我到目前为止所做的代码:

<select id='type' name='type' class="form-control" onchange="loadText(this)">
  <option value="S">S</option>
  <option value="B">B</option>

  <option value="F">F</option>

</select>
<script>
  function loadText(select) {
    alert(select.options[select.valueOf(2)].text);
  }
</script>

我试图获取 selected 选项的值,但在我得到它之前它不起作用

select.selectedIndex

但我希望这个函数只对最后一个值有效,而不是对所有 3

<select id='type' name='type' class="form-control" onchange="loadText(this)">
    <option value="S">S</option>
    <option value="B">B</option>
    <option value="F">F</option>
</select>

<script>
    function loadText(select) {
        var select = document.getElementById('type');

        var lastValue = select.options[select.options.length - 1].value;
        var selectedValue = select.options[select.selectedIndex].text;

        if (lastValue === selectedValue) {
            alert(selectedValue);
        }
    }
</script>