通常我使用$(“#id”).val()来返回所选选项的值,但这一次它不起作用。 所选标记的id为aioConceptName

html代码

<label for="name">Name</label>
<input type="text" name="name" id="name" />

<label for="aioConceptName">AIO Concept Name</label>
<select id="aioConceptName">
    <option>choose io</option>
    <option>roma</option>
    <option>totti</option>
</select>

当前回答

简单明了:

你下拉

<select id="aioConceptName">
    <option>choose io</option>
    <option>roma</option>
    <option>totti</option>
</select>

Jquery代码获取所选值

$('#aioConceptName').change(function() {
    var $option = $(this).find('option:selected');

    //Added with the EDIT
    var value = $option.val(); //returns the value of the selected option.
    var text = $option.text(); //returns the text of the selected option.
});

其他回答

Try

aioConceptName.selectedOptions[0].value

let val = aioconcepts . selecpick [0].value 控制台日志(selected珍惜:“瓦尔); < >标签名,< - >标签 <输入类型=“文本” 选择< id =“aioConceptName > < < / lo选项>选择选项> <罗马选项> < /选项> <选项> totti < /选项> 选择< - >

如果你在事件上下文中,在jQuery中,你可以使用以下方法检索所选的选项元素: $(this).find('option:selected')如下所示:

$('dropdown_selector').change(function() {
    //Use $option (with the "$") to see that the variable is a jQuery object
    var $option = $(this).find('option:selected');
    //Added with the EDIT
    var value = $option.val();//to get content of "value" attrib
    var text = $option.text();//to get <option>Text</option> content
});

Edit

正如possession within所提到的,我的答案只是回答这个问题:如何选择所选的“选项”。

接下来,要获取选项值,使用option.val()。

试试这个价值…

$("select#id_of_select_element option").filter(":selected").val();

或者这个用于文本…

$("select#id_of_select_element option").filter(":selected").text();

您可以使用精确选择的选项进行选择: 下面将给出innerText

$("select#aioConceptName > option:selected").text()

而下面将给你价值。

$("select#aioConceptName > option:selected").val()

我无意中发现了这个问题,并将埃利奥特·博纳维尔的答案简化为:

var conceptName = $('#aioConceptName :selected').text();

或一般:

$('#id :pseudoclass')

这节省了额外的jQuery调用,在一个镜头中选择所有内容,并且更加清晰(我的观点)。