简体   繁体   English

复制元素时,jQuery click事件不会触发

[英]jQuery click event does not trigger when duplicating an element

I have a hidden div which has some content : 我有一个隐藏的div,其中包含一些内容:

<div id="email_content_hidden">
    <div id="email_form">
        <label>De: <span>*</span></label>
        <input type='email' id='email_from' placeholder='De' required="true"/>
        <label>A: <span>*</span></label>
        <input type='email' id='email_to' placeholder='Destinataire' required="true"/>
        <label>Sujet: <span>*</span></label>
        <input type='text' id='email_subject' placeholder='Sujet' required="true"/>
        <label>Message:</label>
        <textarea id='email_message' placeholder='Votre Message'></textarea>
        <div class='email_submit'>Envoyer email</div><div id='email_returnmessage'></div>
    </div>
</div>

Then I have a button which triggers a jsPanel : 然后我有一个按钮触发jsPanel:

$('#new_document').click (function () {
    var content = $('#email_content_hidden').html();

    $.jsPanel({
        content:        content,
        position:       "center",
        theme:          "success",
        title:          "Nouveau document/email",
        size:           {   width:  function(){ return $(window).width()*0.75 }, 
                            height: function(){ return $(window).height()*0.75 } },
        toolbarFooter:  "<i>Popup footer</i>",
    });
});

As you can see I duplicate the code of the hidden div into my jsPanel. 如您所见,我将隐藏的div的代码复制到了jsPanel中。

The problem is that the click event which was assigned to does not fire : 问题是分配给的click事件不会触发:

$('.email_submit').click(function() {
   console.log('submit email');
});

Any idea ? 任何想法 ?

You have to delegate the event, otherwise newly appended elements to the DOM won't have the event bound: 您必须委托事件,否则新添加到DOM的元素将不会绑定事件:

$(document).on('click', '.email_submit', function() {
   console.log('submit email');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM