简体   繁体   English

在以下情况下如何将数据追加到jQuery中的特定div?

[英]How to append data to a particular div in jQuery in the following scenario?

My Jquery function is this 我的Jquery函数是这个

jQuery(document).ready(function(){
            var content='<div class="input-parameters"><div><label>Url</label><div class="inputContainer"><textarea rows="4" cols="25" readonly></textarea></div></div>'+

                    '<div class="saveButtonDiv"><button type="button" class="btn" class="saveButton">Save</button></div>';

            jQuery( "#container" ).on( "click","#saveAndAddNew", function() {
                jQuery('.redirect').append(content);
            });

            var networkContent='<div class="wrapper"><div class="NetworkSelection">+
                                  '<div class="redirect">'+content +'</div>'+
                                  '<div class="buttonContainer">'+
                                  '<button type="button"                              class="btn" id="saveAndAddNew">Add New</button>
                                     </div></div>';
              jQuery('#saveAndAddNetwork').click(function(){
                jQuery('#container').append(networkContent);
            })
        })

and my html is 和我的HTML是

div id="container">
    <div class="wrapper">
        <div class="NetworkSelection">
            <div class="redirect">
                <div class="input-parameters">
                    <div>
                        <label>Redirect Url</label>
                        <div class="inputContainer">
                            <textarea rows="4" cols="25" readonly>

                            </textarea>
                        </div>
                    </div>
                    <div class="clear"></div>
                    <div class="inputParams">
                        <div >
                            <label>Campaign</label>
                            <div class="inputContainer">
                                <input type="text"  class="input-medium"/>
                            </div>
                        </div>
                        <div class="clear"></div>
                        <div>
                            <label>Creative Id</label>
                            <div class="inputContainer">
                                <input type="text" class="input-medium"/>
                            </div>
                        </div>
                        <div class="clear"></div>
                    </div>
                    <div class="saveButtonDiv"><button type="button" class="btn" id="save">Save</button></div>
                    <div class="clear"></div>
                </div>

            </div>
            <div class="buttonContainer">

                <button type="button" class="btn" id="saveAndAddNew">Add New</button>
            </div>

        </div>
    </div>
    <div class="networkButtonContainer">
        <button type="button" class="btn-small">Save</button>
        <button type="button" class="btn-small" id="saveAndAddNetwork">Save & Add Network</button>
    </div>
</div>

What I am trying to do here is on click of 'Add New' button I am appending 'content' to a div with a class ="redirect" and on click of 'save and add network' button I am appending the networkContent to the div with id 'container'. 我要在这里执行的操作是单击“添加新”按钮,然后将“内容”附加到具有类“重定向”的div上,然后单击“保存并添加网络”按钮,我将networkContent附加到div ID为“容器”的div。 Now the problem is if I am clicking 'Add New' button in the div that is appended,the 'content' is added to both,the appended div and also existing div's .I want it to be added to only the div that I am clicking. 现在的问题是,如果我在附加的div中单击“添加新”按钮,则“内容”既添加到附加的div中,也添加到现有的div中。我希望仅将其添加到我所在的div中点击。 How should I acheive this.? 我应该如何实现呢?

The content was appended to all divs that have redirect classes so you have to narrow the context while finding the right place to append. content已附加到所有具有redirect类的div中,因此您必须在缩小上下文的同时找到要附加的正确位置。 For example, try this: 例如,尝试以下操作:

jQuery("#container").on("click", "#saveAndAddNew", function() {
    jQuery(this).closest(".NetworkSelection").find(".redirect").append(content);
});

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

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