<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());
});
Run Code Online (Sandbox Code Playgroud)
对我来说很奇怪,警报消息显示所有选择,但我希望它只显示test4文本.
<select id="testSelection">
<option value="1">test1</option>
<option value="2">test2</option>
.....
</select>
Run Code Online (Sandbox Code Playgroud)
这真的取决于你想要的东西:
$("#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());
});?
Run Code Online (Sandbox Code Playgroud)