如何删除克隆元素 - jquery

时间:2015-08-07 04:51:05

标签: jquery

我想在克隆元素中添加一个删除按钮用于某些目的。

HTML

<div class="more">add</div>
        <div id="other_fee">
         <div>
          <input type="text" value="" name="other" placeholder="Description" />
          <input class="short3 theAmount" type="text" value="" name="other_amount" placeholder="Amount" />
          <div class="inputBlocker"></div>
         </div>
        </div>

jquery

<script type="text/javascript">
      jQuery(function($) {
        $('.more').on('click', function() {
          var $table = $('#other_fee');
          var $tr = $table.find('div').eq(0).clone();
          $tr.appendTo($table).find('input').val('');
        });

        $("#abc").each(function() {
          var form = $(this);

          form.on('keyup', '.theAmount', function() {
            var sum = 0;
            form.find('.theAmount').each(function() {
              sum += +this.value;
            });

            form.find("#other_total").val(sum);
          });
        });
      }); 


</script>

1 个答案:

答案 0 :(得分:0)

您只需添加此代码

即可

到HTML

`<a href=";" class="add">add</a>
 <div class="more hidden">
    <div id="other_fee">
     <div>
      <input type="text" value="" name="other" placeholder="Description" />
      <input class="short3 theAmount" type="text" value="" name="other_amount" placeholder="Amount" />
      <div class="inputBlocker"></div>
     </div>
    </div>

`

到SCRIPT

`$(document).on('click','.add',function(){
  $('.more').toggle('hidden');
});`

//或

 `$(document).on('click','.add',function(){
  if($('.more').hasClass('hidden')){
      $('.more').removeClass('hidden');
  }else{
      $('.more').addClass('hidden');
  }
});`