[英]Getting the Value of an innerHTML of a select tag with JQuery
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.但是每次我打印它只返回一个未定义的值。
jQuery object doesn't have the innerHTML
property, which belongs to the html dom element object. jQuery 对象没有属于 html dom 元素对象的innerHTML
属性。
var option = $("#options option:selected").html();
console.log(option);
You just need:您只需要:
$("#options").val();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id = "options" style = "width:150px;"> <option>Artist Name</option> <option>Track Name</option> <option>Date Uploaded</option> </select>
<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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.