jQuery:选择选择选项后,如何触发事件/执行某些操作? [英] jQuery: How do i trigger event / do something when select option is selected?

查看:81
本文介绍了jQuery:选择选择选项后,如何触发事件/执行某些操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦单击并选择了选择选项,有人可以让我知道触发事件的最佳方法吗?

Can anyone let me know the best way to trigger an event once a select option has been clicked on and chosen?

用户选择了选项后,我想运行一些其他JavaScript.

I want to run some additional JavaScript once the user has selected their option.

推荐答案

使用 $.change() 事件:

$('select').change(function() {
    // Additional JavaScript
});

要获取所选选项的值,请在函数范围内使用$(this).val(),如下所示:

To get the value of the option chosen, use $(this).val() within the scope of the function, like this:

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


如果您使用的是jQuery 1.7+,则可以将其与$.on()一起使用,就像其他任何事件一样:


If you're using jQuery 1.7+, you can use it with $.on() as with any other event:

$('select').on('change', function() {
    // Additional JavaScript
});

这篇关于jQuery:选择选择选项后,如何触发事件/执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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