[英]How can i get the value of the selected option using Javascript or Jquery?
[英]Get selected option data using Jquery or javascript
我必须得到选项值已知的选定选项数据。 我有选择的选项值,我想要包含在选项之间的数据。
例如,在以下选择中:
<select name="oi_report_contact[sex]" id="oi_report_contact_sex">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Other</option>
</select>
我有价值1,我需要通过Jquery或Javascript获取数据“男性”。
请注意: $('#oi_report_contact_sex').val();
当选择1时,将给出1而不是男性。
你只需要打电话
var content = $('#oi_report_contact_sex option:selected').html();
获取所选选项的内部内容。
您可以使用.text()
方法获取文本值。 像这样 。
$('#oi_report_contact_sex').on('change', function () {
alert($('#oi_report_contact_sex').val());
alert($('#oi_report_contact_sex option:selected').text());
});
$("#oi_report_contact_sex").find('option:selected').text();
你可以试试:
$("#oi_report_contact_sex option[value='" + $("#oi_report_contact_sex").val() + "']").text()
jsFiddle链接: http : //jsfiddle.net/ARBb2/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.