繁体   English   中英

jQuery如何使用删除元素删除两个文本框

[英]jQuery how to use a remove elem to remove two text boxes

我正在编写一个脚本,该脚本用于添加/删除表单上的文本框。 这个想法是您输入两个数据块,每个数据块都需要一个文本框。 我想使用一个“删除”按钮删除两个框,但是我还不是jQuery的专家。 谢谢您的帮助!

HTML /脚本:

<form class="smart-green" method="POST" role="form" class="my-form" role="form" method="POST" >
     <table class="materialstab" style="border:1px solid;" cellpadding="5">
         <tr><td><p class="text-box"><label for="materials">Materials</label></p></td>
             <td><p class="text-box"><label for="url">URL</label><a class="add-box" href="#">Add Material</a></p></td></tr>
   </table>

   <script type="text/javascript">
     jQuery(document).ready(function() {
       $('.smart-green .add-box').click(function() { //add box on click
          var n = $('.text-box').length + 1;
          var box_html = $('<tr><td><p class="text-box"><input class="materials" type="text" name="materials'+ n +'" value="" id="materials' + n + '" /><a href="#" class="remove-box">Remove</a>/*ideally I would remove this remove <a> element*/</p></td><td><p class="text-box"> <input type="text" name="url' + n + '" value="" id="url' + n + '" /><a href="#" class="remove-box">Remove</a>/*this <a> would delete both text boxes*/</p></td></tr>'); // HTML for boxes

          box_html.hide();
          $('.smart-green .materialstab tr:last').after(box_html);
          box_html.fadeIn('slow');
          return false;
       });
       $('.smart-green').on('click', '.remove-box', function(){ //remove box
           $(this).parent().css( 'background-color', '#FF6C6C' );
           $(this).parent().fadeOut("slow", function() {
               $(this).remove();
               $('.box-number').each(function(index){
                   $(this).text( index + 1 );
               });
           });
           return false;
        });
     });
 </script>

我正在使用jQuery 3.1.1,如果有帮助的话

通常,您应该用div包围要隐藏的内容-我们将其称为div的id hide_div当我有一个按钮单击事件来隐藏某些东西时,我将使用此代码

$('#id_of_remove_button').on('click',function() {
         $('#hide_div').css('display','none');
});

这样,如果您需要再次显示它,则只需执行.css('display','initial');

将其包围在div中也是不错的选择,因为您只需隐藏一个元素,而不必一个个地隐藏。

如果您实际上想完全删除HTML:

$('#id_of_remove_button').on('click',function() {
         $('#hide_div').html(); // removes everything inside the div
});

就是这样(清理并修改jQuery):

 jQuery(document).ready(function() { $('.smart-green .add-box').click(function() { //add box on click var n = $('.text-box').length + 1; var box_html = $('<tr><td><p class="text-box"><input class="materials" type="text" name="materials'+ n +'" value="" id="materials' + n + '" /></p></td><td><p class="text-box"> <input type="text" name="url' + n + '" value="" id="url' + n + '" /><a href="#" class="remove-box">Remove</a></p></td></tr>'); // HTML for boxes box_html.hide(); $('.smart-green .materialstab tr:last').after(box_html); box_html.fadeIn('slow'); return false; }); $('.smart-green').on('click', '.remove-box', function(){ //remove box $(this).parent().css( 'background-color', '#FF6C6C' ); $(this).parent().fadeOut("slow", function() { $(this).parents('tr').remove(); $('.box-number').each(function(index){ $(this).text( index + 1 ); }); }); return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="smart-green" method="POST" role="form" class="my-form" role="form" method="POST" > <table class="materialstab" style="border:1px solid;" cellpadding="5"> <tr><td><p class="text-box"><label for="materials">Materials</label></p></td> <td><p class="text-box"><label for="url">URL</label><a class="add-box" href="#">Add Material</a></p></td></tr> </table></form> 

暂无
暂无

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

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