简体   繁体   中英

jQuery click event not working on option element

Why doesn't the following code work?

$('select option').live('click', function(){
  alert('hello')
})

html:

<select class="ok">
<option>5</option>
</select>

EXAMPLE: http://jsfiddle.net/fQxj7/2/

Do this instead:

$('select').change(function(){
    alert('hello');
});

If you need to know the selected value inside the event handler, then you can do this:

$('select').change(function() {
    var selectedOption = $(this).val();
});

http://jsfiddle.net/FishBasketGordo/2ABAh/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM