在窗体中获取当前选定的选项

get the currently selected option in a form

本文关键字:选项 窗体 获取      更新时间:2023-09-26

im 尝试使用 JavaScript 在下拉菜单中获取当前选定的选项。 但我似乎无法让它工作

我目前的JS:

var itemqual = $("select#itemqual option:selected").text();
var type = "hat";
$('select#itemtype').change(function() {
if(itemtype == type) {
    $('#graphic').show();
} else {
    $('#graphic').hide();
}
});

但是当我更改选择时,它不起作用。

这个Javascript应该适合你。

var type = "hat";
$('select#itemtype').change(function() {
    if($("select#itemtype option:selected").text() == type) {
        $('#graphic').show();
    } else {
        $('#graphic').hide();
    }
});