Why is my jquery .on('change') not working for dynamically added selects

前端 未结 10 481
既然无缘
既然无缘 2020-12-31 01:12

I\'m adding select elements dynamically, like in the below HTML. I\'m not sure why the .on(\'change\' ...) is not working for the dynamic select. What am I missing?

<
相关标签:
10条回答
  • 2020-12-31 01:36

    Your code:

    $('#x select').on('change', function () { alert('helo'); })
    

    attaches an event handler to the select inside the #x element.

    What you want (from what i understood) is something in the lines of:

    $("#y").on('change','select',function () { alert('helo'); });
    

    This attaches an event handler to the #y element that gets delegated to its children 'select' elements

    From http://api.jquery.com/on/

    The .on() method attaches event handlers to the currently selected set of elements in the jQuery object.

    0 讨论(0)
  • 2020-12-31 01:37

    Event binding to elements that are not in the DOM on initial page load will not work. You need to bind to an element further up the DOM that exists to allow the event to trickle down. This is usually the approach that I take:

    $(document).on({
      change: function() {
        alert('helo');
      }
    }, '#x select');
    
    $(document).on({
      change: function() {
        alert('helo');
      }
    }, '#y select');
    

    I prefer it as you can add subsequent events easily.

    $(document).on({
      change: function() {
        alert('helo');
      },
      blur: function() {
        alert('helo');
      }
    }, '#x select');
    
    0 讨论(0)
  • 2020-12-31 01:38

    The word in square brackets is the alt text, which gets displayed if the browser can't show the image. Be sure to include meaningful alt text for screen-reading software.

    0 讨论(0)
  • 2020-12-31 01:44

    Don't use .live()/.bind()/.delegate(), though. You should use .on().

    for both static and dynamic select changes

    $(document).on('change', 'select', function (e) {
        // do something 
    });
    
    0 讨论(0)
  • 2020-12-31 01:49

    This is easy. Whatever class or id your targeting, try being more precise but no need to include a super parent.

    Example of wrong way:

            $('#tablecontent').on('change', '#tablecontent > table.CampaignGrid > tbody > tr > td', function(e) {
                console.log('IN TABLE CONTENT CHANGE');
                var value = e.target.value;
                $('button#updateLuck').prop('disabled', false).css({'color': '#000', 'font-weight': 600});
    
                //alert($(ele[0]).html());
            });

    CORRECT WAY:

            $('#tablecontent').on('change', 'table.CampaignGrid > tbody > tr > td', function(e) {
                console.log('IN TABLE CONTENT CHANGE');
                var value = e.target.value;
                $('button#updateLuck').prop('disabled', false).css({'color': '#000', 'font-weight': 600});
    
                //alert($(ele[0]).html());
            });

    Notice I dropped the super parent: '#tablecontent'

    It all about clarity.

    • V
    0 讨论(0)
  • 2020-12-31 01:52

    The whole point of .on() is so that you can bind the event to something other than the document. This is what the now depreciated .live() did and it is inefficient in most cases. You are ment to bind the event to the closest parent element that will not be dynamically changed.

    As far as I am aware this is the correct syntax:

    $('#x').on('change', 'select', function(){
        alert('hello');
    });
    

    If #x or #y will be changed, wrap them on an element and bind the event to that.

    0 讨论(0)
提交回复
热议问题