Javascript 使用 JQuery 获取 select 标签的 innerHTML 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12189332/
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
Getting the Value of an innerHTML of a select tag with JQuery
提问by KyelJmD
I have this select tag right here
我这里有这个选择标签
<select id = "options" style = "width:150px;">
<option>Artist Name</option>
<option>Track Name</option>
<option>Date Uploaded</option>
</select>
I am getting the current selected innerHTML value using JQuery
我正在使用 JQuery 获取当前选定的 innerHTML 值
var option = $("#options").innerHTML;
console.log(option);
But each time I print it only returns me a value of undefined.
但是每次我打印它只返回一个未定义的值。
回答by xdazz
jQuery object doesn't have the innerHTML
property, which belongs to the html dom element object.
jQuery 对象没有innerHTML
属性,它属于 html dom 元素对象。
var option = $("#options option:selected").html();
console.log(option);
回答by moonwave99
You just need:
您只需要:
$("#options").val();
回答by Dhanasekar
<form>
<select id = "options" style = "width:150px;">
<option>Artist Name</option>
<option>Track Name</option>
<option>Date Uploaded</option>
</select>
<input type="text" id="optionstext"/>
<input type="text" id="optionshtml"/>
</form>
$('#options').change(function(){
$('#optionstext').val($('#options').text());
$('#optionshtml').val($('#options').html());
});
http://jsfiddle.net/pKsC8/8/