jQuery get selected options from the drop-down list

This translation from: jQuery Dropdown the Get the From the Selected the Option

The I use Usually $("#id").val()to return at The value of the Selected at The the Option, But does not the this Time IT Work. Normally, I use $("#id").val()the return value of the selected option, but this does not work. The selected tag has the id aioConceptName of the selected tag ID for theaioConceptName

Code Html HTML Dai码

<label>Name</label>
<input type="text" name="name" />
<select id="aioConceptName">
    <option>choose io</option>
    <option>roma</option>
    <option>totti</option>
</select>

#1st Floor

Reference: https://stackoom.com/question/iiuv/jQuery get selected options from the drop-down list


#2nd Floor

For dropdown options you probably want something like this: For drop-down options, you might want something like this:

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

Reason at The val()does not do at The Trick IS AN Because clicking the Option does not at The Change at The value of the DropDown - the Just IT ADDS at The :selectedProperty to the Selected the Option at The Which IS A Child . Of the DropDown at The val()cause of the problem is not solved, click option does not change the value of the pull-down menu, it just :selectedadded to the property as a pull-down menu of children of selected options.


#3rd floor

I stumbled across this question and developed a more concise version of Elliot BOnneville's answer: I came across this problem and developed a more concise Elliot BOnneville answer:

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

or generically: or in general:

$('#id :pseudoclass')

This saves you an extra jQuery call, selects everything in one shot, and is more clear (my opinion). This saves you additional jQuery calls, once to select all the contents, but also more clear (I think).


#4th floor

Try this for value ... Try this good value for money ...

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

or this for text ... or the text ...

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

#5th Floor

<select id="form-s" multiple="multiple">
    <option selected>city1</option>
    <option selected value="c2">city2</option>
    <option value="c3">city3</option>
</select>   
<select id="aioConceptName">
    <option value="s1" selected >choose io</option>
    <option value="s2">roma </option>
    <option value="s3">totti</option>
</select>
<select id="test">
    <option value="s4">paloma</option>
    <option value="s5" selected >foo</option>
    <option value="s6">bar</option>
</select>
<script>
$('select').change(function() {
    var a=$(':selected').text(); // "city1city2choose iofoo"
    var b=$(':selected').val();  // "city1" - selects just first query !
    //but..
    var c=$(':selected').map(function(){ // ["city1","city2","choose io","foo"]
        return $(this).text();
    }); 
    var d=$(':selected').map(function(){ // ["city1","c2","s1","s5"]
        return $(this).val();
    });
    console.log(a,b,c,d);
});
</script>

#6th floor

Have you considered using plain old javascript? Have you considered using plain old JavaScript?

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

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

Also See the Getting the Option text AN / value with JavaScript See also use the JavaScript option to obtain text / value

Original articles published 0 · won praise 73 · views 550 000 +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105294037