通常我使用$(“#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>

当前回答

在这种情况下,你最好使用jQuery的change方法来查找当前选择的值,如下所示:

$('#aioConceptName').change(function(){

   //get the selected val using jQuery's 'this' method and assign to a var
   var selectedVal = $(this).val();

   //perform the rest of your operations using aforementioned var

});

我更喜欢这种方法,因为您可以在给定的选择字段中为每个选定的选项执行函数。

希望有帮助!

其他回答

Try

aioConceptName.selectedOptions[0].value

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

你考虑过使用普通的javascript吗?

var box = document.getElementById('aioConceptName');

conceptName = box.options[box.selectedIndex].text;

参见使用JavaScript获取一个选项文本/值

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

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

而下面将给你价值。

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

要获取具有相同class= name的select,您可以这样做,以检查选择选项是否被选中。

var bOK = true;
$('.optKategorien').each(function(index,el){
    if($(el).find(":selected").text() == "") {
        bOK = false;
    }
});

获取所选标签的值:

 $('#id_Of_Parent_Selected_Tag').find(":selected").val();

如果你想获取文本,请使用以下代码:

 $('#id_Of_Parent_Selected_Tag').find(":selected").text();

例如:

<div id="i_am_parent_of_select_tag">
<select>
        <option value="1">CR7</option>
        <option value="2">MESSI</option>
</select>
</div>


<script>
 $('#i_am_parent_of_select_tag').find(":selected").val();//OUTPUT:1 OR 2
 $('#i_am_parent_of_select_tag').find(":selected").text();//OUTPUT:CR7 OR MESSI
</script>