在选择框中获取所选选项标签的值

8

我有一个选择框

<select name="type1">
   <option value="1">Laser Printer</option>
   <option value="2">Line Printer</option>
</select>

现在我有一个id为#New1的按钮,当点击此按钮时,我需要显示所选选项标签之间的值。例如,如果选择激光打印机,我需要获取激光打印机而不是1。

到目前为止我的代码:

alert($(“select [name = type1]:selected”).val()。text()); 但这返回未定义

1个回答

10
您需要获取所选选项的文本:
alert($("select[name='type1'] option:selected").text());

如果您已经将选择器缓存到变量中,则以下语法等同于 alert($('select[name=type1]').find('option:selected').text()); -- alert($mySelect.find('option:selected').text()); - ruffin
此答案不涵盖多选。 - Gelmir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接