jQuery 1.4.2 Using .delegate() on a change event for a doropdown list

元气小坏坏 提交于 2019-12-06 17:49:27

This would be the syntax used with .delegate() for this particular case. Notice that the 'listOfOptions' is the class of the drop-down list.


$('body').delegate('.listOfOptions', 'change', function() {
    if ($(this).find(':selected').attr('class') == 'customOption') {
      // DO SOMETHING!!    
    }
    else {
      // DO SOMETHING ELSE      
    }
  });

It works like a charm in all browsers.

Spacerat

Don't forget to write the delegating in a $(function() {}); block or in $(document).ready(function() {});

It won't work in IE 7-9 otherwise.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!