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

javascript - jQuery live, change in not working in IE6, IE7 - Stack Overflow

programmeradmin27浏览0评论

The code below works as expected in FF but not in IEs...

$(document).ready(function() {

    $('div.facet_dropdown select').live('change', function() {
        var changed_facet = $(this).attr('id');
        var facets = $('select', $(this).closest('form'));
        var args = window.location.href.split('?')[0] + '?ajax=1';
        var clear = false;
        for(var i = 0; i < facets.length; i++) {
            var ob = $(facets[i]);
            var val = ob.val();
            if(clear) {
                val = '';
            }
            args += '&' + ob.attr('id') + '=' + val;
            if(ob.attr('id') == changed_facet) {
                clear = true;
            }
        }

        $.getJSON(args, function(json) {
            for(widget_id in json) {
                var sel = '#field-' + widget_id + ' div.widget';
                $(sel).html(json[widget_id]);
            }
        });

    });

});

The code below works as expected in FF but not in IEs...

$(document).ready(function() {

    $('div.facet_dropdown select').live('change', function() {
        var changed_facet = $(this).attr('id');
        var facets = $('select', $(this).closest('form'));
        var args = window.location.href.split('?')[0] + '?ajax=1';
        var clear = false;
        for(var i = 0; i < facets.length; i++) {
            var ob = $(facets[i]);
            var val = ob.val();
            if(clear) {
                val = '';
            }
            args += '&' + ob.attr('id') + '=' + val;
            if(ob.attr('id') == changed_facet) {
                clear = true;
            }
        }

        $.getJSON(args, function(json) {
            for(widget_id in json) {
                var sel = '#field-' + widget_id + ' div.widget';
                $(sel).html(json[widget_id]);
            }
        });

    });

});
Share Improve this question asked Sep 20, 2009 at 19:12 fabianfabian 1
  • Have you attempted to deduce what is causing the problem at least? – strager Commented Sep 20, 2009 at 19:13
Add a comment  | 

5 Answers 5

Reset to default 24

$.live() does not support the change event:

Currently not supported: blur, focus, mouseenter, mouseleave, change, submit http://docs.jquery.com/Events/live

Try using livequery instead?

Note: jQuery 1.4 now supports the live function for all normal events. It didn't work with IE8 until recently, but I believe this is fixed with jQuery 1.4.2. See this resolved jQuery ticket: IE8 DOES NOT SUPPORT THE CHANGE EVENT WHILE USING LIVE

Use delegate() function instead live(). It the same as live, but supports more events and works fine in IE. In yout case it will be

$('div.facet_dropdown select').delegate('change', function() { ... });

and correspondent undelegate() function

I used -

jQuery('#id').find('select').live("click", function(){
  jQuery(this).change(function(){
    //your code
  });
});

Also note that as of jQuery 1.7 you should use "on" instead of delegate or live.

$(selector).live(events, data, handler);                // jQuery 1.3+
$(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
$(document).on(events, selector, data, handler);        // jQuery 1.7+

Live suffers from a bunch more problems than just the old "no change event" issue: http://api.jquery.com/live/#typefn

发布评论

评论列表(0)

  1. 暂无评论