Similar to val() but for the selected text in a select multiple?
Possible Duplicate:
jQuery get selected text from dropdownlist
<select id="id_deals" name="deals" multiple="multiple">
<option value="1">deal 2</option>
<option value="2">deal 1</option>
</select>
With jquery I can get the value of the selected items like this:
var selected = $(e.target).val();
>> 2
But surprisingly when I try to get the actual selected text (eg deal 1), it gives me both entries:
var selected_text = $(e.target).text();
>> "ndeal 2ndeal 1n"
Why is that and how could I get the text of the selected entries as well?
The jQuery "text()" method returns the inner text of the selected element. In your case you're selecting the whole select tag, so it's giving you all the text inside it (excluding the nested tags themselves). Instead, use:
$("#yourdropdownid option:selected").text();
See this: Get selected text from a drop-down list (select box) using jQuery
e.Target contains the select list! That is why you get the text of both options when calling text()
. If you call val()
, you get the value of the select list, which is the value of one of the options.
上一篇: 选择标签中的jQuery提示文本