javascript - 从面板中删除选定的项目 -JS

标签 javascript jquery

下面是我的代码,它是这样工作的。当用户选择复选框时,它会附加所选的商品名称和价格,并根据用户输入的数量计算小计。

现在,当用户取消选择复选框时,取消选择的项目就会消失,并且总计会减少到旧总计(之前的总计)。

我想要实现的是,我下面的html可以图标fa fa-close,我希望它像取消选择复选框时那样执行。因此,当用户单击该图标时,它会删除相应的项目,并将总数减少到旧的总数

function order(food) {
  var ad = JSON.parse(food.dataset.food),
    existing;
  if (food.checked == true) {
    $('.panel').append(
      '<div class="container" style=" font-size:14px; "> ' +
      '<input type="hidden"  value=' + ad.id + ' data-id="' + ad.id + '"   name="food_id[]" />' +
      '<table style="width:100%;" class="table" id="tables">' +
      '<thead>' +
      '<thead>' +
      '<tbody id="item_list">' +
      '<tr>' +
      '<td  class="icon" ><a href="#"><i class="fa fa-close"></a></i></td>' +
      '<td  class="name" >' + ad.name + '</td>' +
      '<td  class="price" data-price="' + ad.price + '">' + ad.price + '</td>' +
      '<td><p class="total" ><span class="line-total" name="total" id="total"></span></p></td>' +
      '</tr>' +
      '</tbody>' +
      '</table>' +
      '</div>'
    )
  }
} else {
  var total = $(".panel .container [data-id=" + ad.id + "]").parent().find(".total").text();
  $(".panel .container [data-id=" + ad.id + "]").parent().remove();

  if (total) {
    $('.checkout span').text(function(index, oldtext) {
      console.log('this is my old text ' + oldtext)
      return oldtext ? oldtext - total : oldtext;
    });
  }
}



$('.panel').on('keyup', '.quantity', function() {
  order_container = $(this).closest('div');
  quantity = Number($(this).val());
  price = Number($(this).closest('div').find('.price').data('price'));
  points = Number($(this).closest('div').find('.points').data('points'));
  order_container.find(".total span").text(quantity * price);
  order_container.find(".pts-total span").text(quantity * points);
  sum = 0;
  points = 0;
  $(".line-total").each(function() {
    sum = sum + Number($(this).text());
  })
  $(".pts-total").each(function() {
    points = points + Number($(this).text());
  })
  $('.checkout span').text(sum);
});

最佳答案

您可以尝试这个简单的点击事件

$('.panel').on('click','.fa.fa-close',function(){
 $(this).closest('.container').remove();//remove the current element

 var sum = 0;
 $(".line-total").each(function(){
       sum = sum + Number($(this).text());
 });//calculate the new sum 
 $('.checkout span').text(sum);

})

关于javascript - 从面板中删除选定的项目 -JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47899352/

相关文章:

javascript - Jquery DataTables - 触发器显示所有条目

javascript - 获取拖动项目的ID JQuery

ajax - 在 jquery-ajax 函数之后提交

javascript - 如何使用 Javascript 获取 'css-pixel'(媒体查询)宽度?

javascript - 100+列10行渲染性能在ui-grid中是最差的

javascript - 如何禁用对 Iframe 的直接访问

javascript - Firefox 附加组件 pageMod,捕获 contentScriptFile 中完成的 ajax

javascript - For 循环暂停,但立即打印出数组

javascript - CKEditor - 启动后启用 Scayt?

php - DataTables 导出 MySQL 表中的所有记录