2

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".

4

2 回答 2

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

应该做。

于 2013-07-22T18:38:19.397 回答
2

你可以使用这个: -

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>

那么javascript将是这样的:-

   $(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
})
于 2013-07-22T18:41:00.237 回答