简体   繁体   English

使用 onclick 在循环中按 ID 删除元素 - jquery

[英]Remove element by ID in looping using onclick - jquery

I have fetch data (checkbox and text) from database and display it on a html form.我从数据库中获取数据(复选框和文本)并将其显示在 html 表单上。 The data were displayed by using looping.数据通过循环显示。 So, the problem is, I have delete button in the looping and when I delete, it will delete the whole data instead of delete the one I desired.所以,问题是,我在循环中有删除按钮,当我删除时,它会删除整个数据,而不是删除我想要的数据。

All HTML in foreach looping foreach 循环中的所有 HTML

<input type="checkbox" class ="checkbox" name="correct[<?php echo $ans->answerid; ?>][correct]" id="correct[<?php echo $ans->answerid; ?>][correct]" value="<?php echo $ans->correct; ?>" <?php echo $checked; ?>/>

<input type="text" name="answer[<?php echo $ans->answerid; ?>][answer]"  id="answer" class="" value="<?php echo $ans->answer; ?>">

<a class="md-btn-danger" id="remove" ></a>

Jquery查询

$(document).ready(function(){
    $('#remove').click(function(){
        $('#answer').remove();
        $('#remove').remove();
        $('.checkbox').remove();
    });

it supposed to be the delete button only delete the selected one by the checkbox and text id它应该是删除按钮,只删除复选框和文本 id 选定的一个

I think your data is dynamically added.我认为您的数据是动态添加的。 Then don't use IDs because IDs should be unique然后不要使用 ID,因为 ID 应该是唯一的

You should use class names on remove anchors.您应该在移除锚点上使用类名。 Then remove elements using prev() or next() function然后使用prev()next()函数remove元素

 $(document).ready(function(){ $('.remove').click(function(){ $(this).prev().remove(); //Remove input $(this).prev().remove(); // Remove checkbox $(this).remove(); // Remove this }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" class ="checkbox" name="correct[<?php echo $ans->answerid; ?>][correct]" id="correct[<?php echo $ans->answerid; ?>][correct]" value="<?php echo $ans->correct; ?>" checked/> <input type="text" name="answer" id="answer" class="" value="answer"> <a class="md-btn-danger remove" >Remove</a><br> <input type="checkbox" class ="checkbox" name="correct[<?php echo $ans->answerid; ?>][correct]" id="correct[<?php echo $ans->answerid; ?>][correct]" value="<?php echo $ans->correct; ?>" checked/> <input type="text" name="answer[<?php echo $ans->answerid; ?>][answer]" id="answer" class="" value="<?php echo $ans->answer; ?>"> <a class="md-btn-danger remove" >Remove</a>

Or add parent element:或者添加父元素:

 $(document).ready(function(){ $('.remove').on("click",function(){ $(this).parent().remove(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <input type="checkbox" class ="checkbox" name="correct[<?php echo $ans->answerid; ?>][correct]" id="correct[<?php echo $ans->answerid; ?>][correct]" value="<?php echo $ans->correct; ?>" checked/> <input type="text" name="answer" id="answer" class="" value="answer"> <a class="md-btn-danger remove" >Remove</a><br> </div> <div> <input type="checkbox" class ="checkbox" name="correct[<?php echo $ans->answerid; ?>][correct]" id="correct[<?php echo $ans->answerid; ?>][correct]" value="<?php echo $ans->correct; ?>" checked/> <input type="text" name="answer[<?php echo $ans->answerid; ?>][answer]" id="answer" class="" value="<?php echo $ans->answer; ?>"> <a class="md-btn-danger remove" >Remove</a></div>

check the heirarchy you have given as parentNode.检查您作为 parentNode 给出的层次结构。 You are removing the parent node which holds the whole data.您正在删除包含整个数据的父节点。 So, give relation correctly.所以,正确给出关系。 This might only be the problem.这可能只是问题所在。 Otherwise it alright.否则没关系。 If it does not work please share your bit of code containing that part.....如果它不起作用,请分享您的包含该部分的代码.....

我的建议是只给 parentNode 一次,而不是在 removeChild() 方法中。

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

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