javascript JQuery 检索文本而不是选择列表的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10226065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery retrieving text instead of value of select list
提问by Deviland
I need to get the value from a select list but JQuery is returning the text within the options of the select.
我需要从选择列表中获取值,但 JQuery 正在返回选择选项中的文本。
I have the following simple code.
我有以下简单的代码。
<select id="myselect">
<option selected="selected">All</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
I then use the following JQuery, which I thought would get me the value
然后我使用以下 JQuery,我认为这会给我带来价值
var myOption = $('#myselect').val()
but when I look at myOption
I get the text of 'One' or 'two'?
但是当我看myOption
我得到“一”或“二”的文字时?
回答by GeckoTang
update : add val().
更新:添加 val()。
console.log($('#myselect').val());
// all option's value
$('#myselect').find('option').each(function(){
console.log($(this).text());
console.log($(this).val());
});
// change event
$('#myselect').change(function(){
console.log($(this).find(':selected').text());
console.log($(this).find(':selected').val());
});
? demo : http://jsfiddle.net/yLj4k/3/
? 演示:http: //jsfiddle.net/yLj4k/3/
回答by Hugo Delsing
I had this problem because the jQuery Val()
function was in my head and I actually defined the options as <option val='xx'>
instead of <option value='xx'>
. That's what is causing the problem. See this updated jsfiddle.
我遇到了这个问题,因为 jQueryVal()
函数在我的脑海中,我实际上将选项定义<option val='xx'>
为<option value='xx'>
. 这就是导致问题的原因。请参阅此更新的jsfiddle。
<select id="myselect">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<select id="myselect2">
<option val="1">One</option>
<option val="2">Two</option>
</select>
<script>
$('select').change(function() {
alert($(this).val());
});
</script>
The first select
will alert "1" or "2" and the second select
will alert "One" or "Two".
第一个select
将警告“1”或“2”,第二个select
将警告“一”或“二”。
回答by Pranay Rana
to get the text value easiest way is
获取文本值最简单的方法是
For selected option Text :
对于选定的选项文本:
$("#myselect option:selected").text();
For selected option value:
对于选定的选项值:
$("select#myselect").val();
回答by Tats_innit
edit
编辑
Hiya
日亚
working demo here: using text http://jsfiddle.net/QtjTq/3/&& using val here http://jsfiddle.net/QtjTq/4/
这里的工作演示:使用文本http://jsfiddle.net/QtjTq/3/&& 在这里使用 val http://jsfiddle.net/QtjTq/4/
like this for accessing text() - var myOption = $('#myselect option:selected').text()
for accessing value - var myOption = $('#myselect option:selected').val()
像这样访问 text() - var myOption = $('#myselect option:selected').text()
访问值 - var myOption = $('#myselect option:selected').val()
to elaborate more:
详细说明:
for valueif you want to access value=
attribute use .val()
;
如果您想访问value=
属性 use .val()
,则为 value;
for textif you want to get the text i.e.one or two
then use selected options ... option:selected
with .text()
api.
对于文本,如果您想获取文本,即one or two
然后使用... option:selected
带有.text()
api 的选定选项。
further if you want to read http://forum.jquery.com/topic/jquery-using-val-vs-text
如果你想进一步阅读http://forum.jquery.com/topic/jquery-using-val-vs-text
hope this helps :) cheers
希望这会有所帮助:) 欢呼
var myOption = $('#myselect option:selected').text() //**or whatever suit you**
var myOption = $('#myselect option:selected').val()
<select id="myselect">
<option value="1">One</option>
<option value="2">Two</option>
</select>