通常我使用$(“#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中,你可以使用以下方法检索所选的选项元素: $(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()。

其他回答

获取所选标签的值:

 $('#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>

试试这个价值…

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

或者这个用于文本…

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

如果你在事件上下文中,在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()。

通常,您不仅需要获取选定的值,还需要运行一些操作。那么,为什么不避免所有的jQuery魔法,而只是将所选值作为参数传递给动作调用呢?

<select onchange="your_action(this.value)">
   <option value='*'>All</option>
   <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.
});