Dynamically created select menu on change not working

前端 未结 4 1627
感动是毒
感动是毒 2020-12-31 20:57

I have a jsfiddle here - http://jsfiddle.net/FTHVJ/

And demo here - http://ttmt.org.uk/select

I know this looks a little crazy but it\'s cloest I could get t

相关标签:
4条回答
  • 2020-12-31 21:22

    For dynamically created elements, you need to use the .on function:

    $('#new_select').on('change', '#menu_Two', function(){
        alert('here');
    });
    

    Event listeners can only be attached to elements that are in the DOM, not elements that don't exist at the time.

    0 讨论(0)
  • 2020-12-31 21:26

    You shouldn't add the event handler until you've added the DOM node:

    $(function(){
    
      var select_two = "<select id='menu_Two'>";
          select_two += "<option>Menu Two 1</option>";
          select_two += "<option>Menu Two 2</option>";
          select_two += "<option>Menu Two 3</option>";
          select_two += "<option>Menu Two 4</option>";
          select_two += "</select>";
    
    
      $('#menu_One').on('change', function(){
        $('#new_select').append(select_two);
        $('#menu_Two').on('change', function(){
          alert('here');
        })
      });
    });
    

    With fiddle: http://jsfiddle.net/cjcLU/

    0 讨论(0)
  • 2020-12-31 21:39

    this is how I did it

    $(document).on('change', "body", function(){
                $( ".ui-selectmenu" ).selectmenu();
            });
    
    0 讨论(0)
  • 2020-12-31 21:45

    Try:

    $(document).on('change', '#menu_Two', function(){
            alert('here');
          });
    

    DEMO FIDDLE

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