admin 管理员组文章数量: 1086019
I have this HTML:
<select class="business_types_select" name="business_type_id">
<option value="0" selected="selected">Select a Business Type</option>
<option value="54">Bakery</option>
<option value="55">Tobacco</option>
</select>
and if I select Bakery and then try:
$(".business_types_select").val()
I get 54, but how do i get the text Bakery? If I try
$(".business_types_select").text()
I get:
" Select a Business Type Bakery Tobacco "
I have this HTML:
<select class="business_types_select" name="business_type_id">
<option value="0" selected="selected">Select a Business Type</option>
<option value="54">Bakery</option>
<option value="55">Tobacco</option>
</select>
and if I select Bakery and then try:
$(".business_types_select").val()
I get 54, but how do i get the text Bakery? If I try
$(".business_types_select").text()
I get:
" Select a Business Type Bakery Tobacco "
Share
Improve this question
edited May 5, 2011 at 18:29
Nic
13.8k7 gold badges42 silver badges43 bronze badges
asked May 5, 2011 at 18:12
Matt ElhotibyMatt Elhotiby
44.1k91 gold badges224 silver badges328 bronze badges
1
- possible duplicate of jquery get selected text from dropdownlist – Naftali Commented May 5, 2011 at 18:19
5 Answers
Reset to default 11Try:
$(".business_types_select :selected").text()
Without this, the .text()
is behaving like it should, which is to bring back all of the text from that particular class. Adding :selected
to the selector narrows it down to just the one you have selected.
Remember to cache your selectors if you're going to be operating on them often for performance.
You have to reduce the selector to the selection option
:
$(".business_types_select").find('option:selected').text();
Like this:
$(".business_types_select > option:selected").text()
(In the short example here I have assumed that there is only one <select class="business_types_select" />
. My jsfiddle example is more prehensive.)
$(".business_types_select option:selected").text()
That will return the text of the selected option only
$(".business_types_select").find('option:selected').text();
本文标签: javascripthow do I get the text instead of the valueStack Overflow
版权声明:本文标题:javascript - how do I get the text instead of the value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744055485a2525770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论