admin管理员组文章数量:1429091
I am using dropdown and I want to get value of selected option.
I am able to do that using jquery html() method.
But in case selected option value is containing html encodable character lets say it is &(ampersand.)
alert('html() : ' + $('#dropdown option:selected').html());
<script src=".11.0/jquery.min.js"></script>
<select id="dropdown">
<option selected>Test & Get knowledge</option>
</select>
I am using dropdown and I want to get value of selected option.
I am able to do that using jquery html() method.
But in case selected option value is containing html encodable character lets say it is &(ampersand.)
alert('html() : ' + $('#dropdown option:selected').html());
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="dropdown">
<option selected>Test & Get knowledge</option>
</select>
Then this & is encoded to &a m p;
How to treat this value as only text?
Share Improve this question edited Dec 22, 2015 at 10:59 V.J. asked Dec 22, 2015 at 10:54 V.J.V.J. 9283 gold badges18 silver badges37 bronze badges 1- You can use, .text() in that case. – Sahil Commented Dec 22, 2015 at 11:28
5 Answers
Reset to default 3https://jsfiddle/programtheweba/8gohx47g/
By using Jquery text(), we will get only text and characters are not encoded.
alert('text() : ' +$('#dropdown').text());
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="dropdown">
<option selected>Test & Get knowledge</option>
</select>
&
is a special character in HTML (it's used for entities) and cannot appear as a normal character.
Your HTML is invalid (&x
is not an entity), but the browser automatically corrects it to&
as it is parsed.
jQuery faithfully returns the fixed-up HTML when you ask it to.
Source
Hence use .text()
instead of .html()
:
$('#dropdown option:selected').text();
You can also use .val()
along with select element selector to get selected options text value:
$('#dropdown').val();
You can use jquery .text() instead:
console.log('text() : ' + $('#dropdown option:selected').text());//prints out text() : Test & Get knowledge
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropdown">
<option selected>Test & Get knowledge</option>
</select>
You need to use .text()
instead of .html()
:
$('#dropdown option:selected').text();
Or use .val()
$('#dropdown').val();
- Include
jQuery
in your code. - Wrap your code in DOM Ready
$(function(){ ... })
- Use
$('#dropdown option:selected').val()
to fetch the value.
$(function(){
alert('html() : ' + $('#dropdown option:selected').val());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropdown">
<option selected>Test & Get knowledge</option>
</select>
&
will be parsed as &
only and will not be changed.
本文标签: javascriptHow to get selected option value of dropdown using jqueryStack Overflow
版权声明:本文标题:javascript - How to get selected option value of dropdown using jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745526530a2661859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论