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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:21:14  来源:igfitidea点击:

Getting the Value of an innerHTML of a select tag with JQuery

javascriptjqueryhtmlselect

提问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 innerHTMLproperty, 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/