如何打印html下拉列表菜单中只有一个选项的选择值



你好,我有这个下拉列表菜单,我想做一个脚本,当我只选择最后一个选项的标签在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>

我试图得到所选选项的值,但它不工作,之前我有它

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>

相关内容

最新更新