1 回答
TA贡献1848条经验 获得超2个赞
您删除按钮不起作用,因为您正在添加,EventListener因此custom-button只有在单击后它才会起作用custom-button。
您可以尝试使用以下代码class来代替id元素。
$(document).ready(function(){
$('body').on('click','#custom-button',function(){
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>';
$('.fillin-answer').append(new_input);
});
$('body').on('click','.remove',function(){
$(this).closest('.blanksanswers').remove();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>
- 1 回答
- 0 关注
- 82 浏览
添加回答
举报