jQuery: live change event on IE7

别说谁变了你拦得住时间么 提交于 2019-12-08 06:14:50

问题


I have capture change event on drop down select. This drop down field is created using AJAX so event capture need to be done with live or bind. Here I add the code which I'm using currently. This is working on fire fox. On IE7 its not working. Can anyone tell me a way to capture live event of drop down select on IE7


    <select id="lob_drop" name="opt" class="select">
       <option value="1">option 1</option>
       <option value="2">option 2</option>
       <option value="3">option 3</option>
    </select>

    jQuery('#lob_drop').live('change',function(){
       alert(jQuery('#lob_drop option:selected').val());
    });


回答1:


Use jquery .on(). .live() is deprecated.

jQuery(document).on('change', '#lob_drop', function(){
       alert(jQuery('#lob_drop option:selected').val());
   }
);

Demo




回答2:


What is your JQuery version ?

It works for me on IE7 using JQuery 1.4+ It seems live() wouldn't work with change event in IE in all previous versions.




回答3:


Use the following:

$('body').on('change', '#lob_drop', function(){
   alert($(this).val());
});

Here's a fiddle with an example: http://jsfiddle.net/7EcGE/24/

My previous suggestion below won't work since .live is deprecated an removed since jQuery 1.9:

jQuery('#lob_drop').live('change',function(){
   alert(jQuery(this).val());
});


来源:https://stackoverflow.com/questions/9903287/jquery-live-change-event-on-ie7

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