简体   繁体   中英

How to get the text value of the selected option of a select jquery object?

if I'm passing a jquery object that I know is a select object, how can I get the text (not the value) of the option that is selected?

I'm needing something like this.

...function ($select){

    var selectedText = $select.selected().text();

}

And since $select is already a jquery object, I cant really change the selector of the object to use ":selected".

$select.find(':selected').text();

应该做的。

You can use this:-

Suppose you have a dropdown like this:-

    <select id="dropdown">
        <option value="aa">a</option>
        <option value="bb">b</option>
        <option value="cc">c</option>
    </select>

then javascript will be like this:-

   $(document).ready(function() {
        obj = $('#dropdown :selected')
        value = obj.val()
        alert(value) # will alert aa/bb/cc 

        text = obj.text()
        alert(text) # will alert a/b/c
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM