jQuery cloning html, update the DOM?

前端 未结 2 1625
灰色年华
灰色年华 2021-01-15 06:47

I need help of Javascript / jQuery experts to solve the next problem:

---- 1. this Javascript alerts the id of a selected option in a select html tag:

         


        
相关标签:
2条回答
  • 2021-01-15 06:48

    Have you tried .clone(true) which clones all the handlers attached? It's described at the bottom of the Clone documentation.

    0 讨论(0)
  • 2021-01-15 07:09

    The jQuery $("#...") syntax will return the first matched element by exact id. If you are cloning elements but not differentiating them by their id, this code will not work as you expect it to.

    You can compare the differences between the following two expressions:

    alert($("#id_productos_list").size());
    

    ...and

    alert($("[id='#id_productos_list']").size());
    

    The first expression will return either zero or one depending on the presence of an element with id "id_productos_list" in your page. The first element in declared order wins.

    The second expression will return zero or one or more depending on the the set of all elements with id "id_productos_list" in your page.

    Also important to note is that it doesn't appear that the event handlers are copied over as part of the clone() operation. You may need to reassign these handlers to the new elements.

    var newElement = $(selector).clone(true);
    
    0 讨论(0)
提交回复
热议问题