猿问

删除从 php 获取的元素,该元素与 jquery 动态创建的输入结合在一起

我正在尝试通过单击按钮动态添加输入。有一个删除输入按钮以及动态创建的输入元素。这将删除输入元素。我已经实现了这一点,并将输入值进一步插入到 mysql 数据库中。我对此没有问题。


现在,当我从数据库(while 循环)获取现有值时,现有输入值将与相同的删除按钮一起创建。单击删除按钮不会删除该元素,但是当我单击插入输入然后删除时,它会起作用。


我找不到解决这个问题的方法。如果有人可以帮忙的话。下面是我的代码。


我的 HTML


<span id="insertblank">

    <button type="button" class="btn" id="custom-button">Insert Input</button>

</span>


<div class="col-md-12 form-group fillin-answer">

  <!-- My PHP WHILE loop would actually be here, but for illustration, I have manually inserted the elements as below -->

   <!-- BEGIN INSERTED ELEMENTS -->

   <div class="blanksanswers" id="removeans1">

     <label class="removeans1">

        Input <span>1</span><span class="text-danger">*</span>

        <button type="button" class="btn btn-sm btn-danger remove" data-id="1">

          Remove 1 Input

        </button>

     </label>

     <div class="input-group removeans1">

       <input class="form-control" id="fillin_answer1" name="fillin_answer[]" value="some values"/>

     </div>

   </div>

   <!--END INSERTED ELEMENTS-->

</div>

查询


var count = 0;

var customButton = document.querySelector('#custom-button');

           customButton.addEventListener('click', function(event) {

               count++;

                   $('.fillin-answer').append(new_input);

                   $('.removeans'+count).on("click",".remove", function(e){ 

                       e.preventDefault();

                       var id = this.getAttribute("data-id");

                       $("#removeans"+id).remove();

                       if($(".blanksanswers").length == 0) {

                           count=0;

                           questc=0;

                       }

                   })

               //}

           });

我的代码笔在这里

感谢任何帮助。


慕容708150
浏览 91回答 1
1回答

慕尼黑5688855

您删除按钮不起作用,因为您正在添加,EventListener因此custom-button只有在单击后它才会起作用custom-button。您可以尝试使用以下代码class来代替id元素。$(document).ready(function(){&nbsp; $('body').on('click','#custom-button',function(){&nbsp; &nbsp; var new_input = '<div class="blanksanswers" ><label class="removeans" style="color:#333D79FF; font-size:15px;">Input <span></span><span class="text-danger">*</span><button type="button" class="btn btn-sm btn-danger remove">Remove Input</button></label><div class="input-group removeans"><input class="form-control" id="fillin_answer" name="fillin_answer[]" /></div></div>';&nbsp; &nbsp; $('.fillin-answer').append(new_input);&nbsp; });&nbsp;&nbsp;&nbsp; $('body').on('click','.remove',function(){&nbsp; &nbsp; $(this).closest('.blanksanswers').remove();&nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><span id="insertblank">&nbsp; &nbsp; <button type="button" class="btn" id="custom-button">Insert Input</button></span><div class="col-md-12 form-group fillin-answer">&nbsp; <!-- My PHP WHILE loop would actually be here, but for illustration, I have manually inserted the elements as below -->&nbsp; &nbsp;<!-- BEGIN INSERTED ELEMENTS -->&nbsp; &nbsp;<div class="blanksanswers" id="removeans1">&nbsp; &nbsp; &nbsp;<label class="removeans1">&nbsp; &nbsp; &nbsp; &nbsp; Input <span>1</span><span class="text-danger">*</span>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-sm btn-danger remove" data-id="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Remove 1 Input&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp;</label>&nbsp; &nbsp; &nbsp;<div class="input-group removeans1">&nbsp; &nbsp; &nbsp; &nbsp;<input class="form-control" id="fillin_answer1" name="fillin_answer[]" value="some values"/>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp;</div>&nbsp; &nbsp;<!--END INSERTED ELEMENTS--></div>
随时随地看视频慕课网APP
我要回答