简体   繁体   English

jQuery 事件时 select 选项

[英]jQuery event when select option

What's the event to bind for when a select form is selected?选择 select 表单时要绑定的事件是什么?

I have something like this:我有这样的事情:

<select id='list'>
<option value='1'>Option A</option>
<option value='2'>Option B</option>
<option value='3'>Option C</option>
</select>

When Option B is selected, I want some function to run.选择选项 B 后,我希望运行一些 function。

So what do I bind,那我绑定什么,

$("#list").bind("?", function (){
// How do I check if it's option b that's selected here
//blah blah
});

This jQuery snippet will get you started: 这个jQuery代码段将帮助您入门:

$('#list').change(function() {
    if ($(this).val() === '2') {
        // Do something for option "b"
    }
});

the event you are looking for is change . 你正在寻找的事件是change more info about that event is available in the jquery docs here: http://docs.jquery.com/Events/change#fn 有关该事件的更多信息,请参见jquery文档: http//docs.jquery.com/Events/change#fn

Although few years late, this actually works also if one select already selected option.虽然晚了几年,但如果一个 select 已经选择了选项,这实际上也有效。

jQuery('#list').on('click', 'option', function () {
    if (jQuery(this).val() === '2') {
        // Do something for option "b"
    }
});

Maybe select() is a more accurated solution:也许select()是一个更准确的解决方案:

$('#list').select(function() {
    if ($(this).val() === '2') {
        // Do something for option "b"
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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