最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Event name when select an option inside a select - Stack Overflow

programmeradmin3浏览0评论

I know the event name when we change something in a select is change (html: onchange), but I would like to know what is the event name when I select (click) a specific option.

Example :

<select>
    <option>Option 1</option>
</select>

When I click on Option 1, what event happen ? Only change (on the select itself and all option) or something else ?

Thanks.

I know the event name when we change something in a select is change (html: onchange), but I would like to know what is the event name when I select (click) a specific option.

Example :

<select>
    <option>Option 1</option>
</select>

When I click on Option 1, what event happen ? Only change (on the select itself and all option) or something else ?

Thanks.

Share Improve this question edited Jun 5, 2012 at 16:26 JohnFx 34.9k18 gold badges107 silver badges166 bronze badges asked Jun 5, 2012 at 16:19 David BélangerDavid Bélanger 7,4385 gold badges41 silver badges55 bronze badges 1
  • In most browsers change. – mplungjan Commented Jun 5, 2012 at 16:23
Add a ment  | 

4 Answers 4

Reset to default 7

By default, in most browser happen change event when you change select and click event for option.

$('select').on('change', function(event) {
  console.log(event.type);   // event.type is to get event name
});

'change' on the SELECT and 'click' on the OPTION. Also, in Opera, 'input' might fire on the SELECT too if you add the listener with addEventListener() for example.

event.type returns the nature of event. Refer the Demo

$('select').bind('change', function(event) {
    alert("Event :"+ event.type);   
});

You can use .on/.bind on the element.​

You can put it in the onclick event of the option you are interested in like so:

<select id="MySelect">
    <option id="optionA" onclick="alert('You clicked A')">a</option>
    <option id="optionB">b</option>  
</select>
发布评论

评论列表(0)

  1. 暂无评论