如何使用jQuery捕获<select>的更改事件?

omg*_*omg 2 jquery event-handling

<select id="target">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
</select>
Run Code Online (Sandbox Code Playgroud)

说,我想根据选定的值进行一些处理.

set*_*eth 13

$('#target').change( function() {
   $(this).find(":selected").each(function () {
            console.log( $(this).val() );
    });
 });
Run Code Online (Sandbox Code Playgroud)


RSo*_*erg 6

$("#target").change( function() {
    alert($("#target option:selected").val());
});
Run Code Online (Sandbox Code Playgroud)

如果此人不更改选项,您可能还需要考虑这一点,只需单击下拉菜单并选择相同的项目:

$("#target option").click( function() {
     alert($("#target option:selected").val());
});
Run Code Online (Sandbox Code Playgroud)