<select id="testSelection">
<option> test1 </option>
<option> test2 </option>
<option> test3 </option>
<option> test4 </option> <--- selected this one from the pull down menu
<option> test5 </option>
<option> test6 </option>
</select>
$("#testSelection").change(function()
{
alert($(this).text());
});
对我来说很奇怪,警报消息显示了所有选择,但我希望它只显示在 test4 文本上。
最佳答案
<select id="testSelection">
<option value="1">test1</option>
<option value="2">test2</option>
.....
</select>
这实际上取决于您想要什么:
$("#testSelection").on('change', function() {
// if you just want the text/html inside the select (ie: test1, test2, etc)
alert($(this).find('option:selected').text());
// or you want the actual value of the option <option value="THISHERE">
alert($(this).val());
});
关于jquery - 如何使用jquery获取下拉菜单中选定选项的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12080002/