[英]How to get the value of a select box element by its option in javascript?
My select box is as follows: 我的选择框如下:
<select id='list'>
<option value='1'>A</option>
<option value='2'>B</option>
<option value='3'>C</option>
</select>
I want to get the value of option - B without selecting it. 我想获取选项-B的值而不选择它。
With jQuery I can do the opposite ie get the text when I know the value through following: 使用jQuery,我可以做相反的事情,即当我通过以下方法知道值时获取文本:
$("#list option[value='2']").text();
But I want the reverse. 但我要相反。 I tried with following but not working:
我尝试了以下但不起作用:
$("#owner option['B']").val();
How can I do this? 我怎样才能做到这一点?
您可以使用:contains(text)
选择器,仅获取包含某些文本的元素:
$("#list option:contains('B')").val();
try this 尝试这个
var b = $("#list").find('option:contains("B")').val();
see here : http://jsfiddle.net/MxpTZ/1/ 看到这里: http : //jsfiddle.net/MxpTZ/1/
Try this script 试试这个脚本
$('#list option:contains("B")').prop('value') //jquery 1.6 or later
oR 要么
$('#list option:contains("B")').attr('value');
如果要按位置获取元素,可以执行以下操作:
$('#list').children().eq(1).val();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.