简体   繁体   English

在jQuery中添加和删除div不起作用

[英]Adding and removing div in jquery not working

I am trying to add question box as shown below, the first remove button works fine. 我试图添加问题框,如下所示,第一个删除按钮可以正常工作。 But when I dynamically add the div and then try to remove it does not work. 但是当我动态添加div然后尝试删除它时不起作用。 I tried to alert on deleting question it still does not work. 我试图提醒删除问题,但仍然无法正常工作。 Can anyone help me on this ? 谁可以帮我这个事 ?

JS JS

<script type="text/javascript">
jQuery(document).ready(function(e) {

    jQuery( '#add-question' ).on('click', function() {
        var lastId = jQuery('.del-question').last().data('id');
        var nextId = lastId+1;
        var questionBox = '<div class="question-box box-1"><h2>Question 1</h2><input type="text" name="ques_title" class="ques_title" placeholder="Enter question title" value=""><div class="form-group"><a class="del-question button" href="#" data-id="1">Remove</a></div></div>';
        jQuery('.question-box').after(questionBox);
        return false;
    });

    jQuery( '#quiz-box .del-question' ).on('click', function() {
        var id = jQuery(this).data('id');
        jQuery('.box-'+id).remove();
        return false;
    });
});
</script>

HTML HTML

<div id="quiz-box">
  <div class="question-box box-1">
   <h2>Question 1</h2>
   <input type="text" name="ques_title" class="ques_title" placeholder="Enter question title" value="">
   <div class="form-group">
    <a class="del-question button" href="#" data-id="1">Remove</a>
   </div>
 </div>
 <div class="form-group">
  <a id="add-question" class="button" href="#">Add</a>
 </div>

I made a Snippet for you. 我为您制作了一个代码段。

The problem was that you can't bind events to dynamic objects. 问题是您不能将事件绑定到动态对象。 $(document).on is needed. $(document).on是必需的。

Plus, I used clone() and added a "renameQuestions" function, just to give you a better way to approach what you want. 另外,我使用了clone()并添加了“ renameQuestions”函数,只是为您提供了一种更好的方法来处理您想要的内容。

 $(document).ready(function(e) { function addQuestion(){ var question = $('#question-template').clone(); question.css("display","block").removeAttr('id'); $('#questions').append(question); } function renameQuestions(){ $('.question-box').each(function(i,v){ $(this).find('.question_id').html(i); }); } $('#add-question').on('click', function() { addQuestion(); renameQuestions(); }); $(document).on('click','.del-question', function() { $(this).closest('.question-box').remove(); renameQuestions(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="questions"> <div class="question-box" id="question-template" style="display:none;"> <h2>Question <span class="question_id"></span></h2> <input type="text" name="ques_title" class="ques_title" placeholder="Enter question title" value=""> <div class="form-group"> <a class="del-question button" href="#">Remove</a> </div> </div> </div> <div class="form-group" style="margin-top:20px;"> <a id="add-question" class="button" href="#">Add</a> </div> 

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

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