在选择框中获取所选选项标签的值 [英] Getting the value of the selected option tag in a select box

查看:62
本文介绍了在选择框中获取所选选项标签的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择框

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

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

Now I have a button with id #New1 when this button gets clicked I need to display the value between the option tag that was selected. Eg if Laser Printer is selected I need to get Laser Printer and not 1

到目前为止我的代码

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

推荐答案

您需要获取所选选项的文本:

You need to get the text of the selected option:

alert($("select[name='type1'] option:selected").text());

这篇关于在选择框中获取所选选项标签的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆