jQuery click事件不适用于选项元素 [英] jQuery click event not working on option element

查看:80
本文介绍了jQuery click事件不适用于选项元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码为什么不起作用?

Why doesn't the following code work?

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

html:

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

示例:: 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/

这篇关于jQuery click事件不适用于选项元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆