JQuery基于Selection在表中添加新行,但是每次最新选择都会覆盖整个表

时间:2018-02-14 13:26:36

标签: php jquery laravel-5.3 selectionchanged

我不擅长javascript框架我在Laravel中做这个项目

如果需要该代码中的任何内容,我将提供

我想要实现的是当用户从下拉列表中选择一个值时,该行是动态添加的行,其工作完全正常。

现在,此行包含有关所选项目的数据,其名称描述价格以及首次正常工作时的数量和金额

然后将新产品添加到购物车中,该产品还向表中添加了一行,但之前的产品记录也替换为此产品值。

产品的首次图像选择 enter image description here

其他产品的第二张图片选择 enter image description here

我们去了第一个产品。和产品变化,最新产品覆盖了整个表格及其数据,之前已经消失了

注意

我根据我指定的唯一类在文本字段中设置了值。

Jquery代码

$('.ProductClass').delegate('.Product','change', function () {
  var inventory = $('.Product').html();
  var qty=1; 

  var n = ($('.neworderbody tr').length - 0) + 1;
  var tr = '<tr><td><input type="text" class="inventId form-control" name="inventId[]" readonly></td>' +

  '<td ><input type="number" class="qty form-control"  name="qty[]" ></td>' +

  '<td style="display: none;"><input type="text" class="costprice form-control" name="costprice[]" readonly></td>' +

  '<td><input type="text" class="price form-control" name="price[]" readonly></td>' +

  '<td><input type="text" class="amount form-control" name="amount[]" readonly></td>' +

  '<td><span class="fa fa-trash delete" data-toggle="tooltip" data-original-title="Remove Item" value="x" style="margin-left: 36px;margin-top: 14px;"></span></td></tr>';
  $('.neworderbody').append(tr);


        var tr = $(this).parent().parent();
        var inventory = $('.Product option:selected').attr('data-name');
        console.log(inventory);
        var SalesPrice = $('.Product option:selected').attr('data-price');
        var CostPrice = $('.Product option:selected').attr('cost-price');
        var description = $('.Product option:selected').attr('data-pro');
        $('.inventId').val(inventory);
        $('.price').val(SalesPrice);         

        $('.qty').val(qty);
        var qty = tr.find('.qty').val() - 0;
        var price = tr.find('.price').val() - 0;

        var total=qty*price;
        tr.find('.amount').val(total);
  //console.log(qty+"--------"+price);

});

非常感谢你帮助我

1 个答案:

答案 0 :(得分:0)

感谢大家,关于我的问题

我找到了一个在文本字段中嵌入值的解决方案,所以我只是简单地获取变量的值,然后将值分配给表中的特定文本字段

完美无缺

如果有人有好的解决方案,请分享

<强>代码

   $('.ProductClass').delegate('.Product','change', function () {
  var inventory = $('.Product').html();
  var qty=1; 
  var inventory=null;
  var n = ($('.neworderbody tr').length - 0) + 1;
  n=1;
  var inventory = $('.Product option:selected').attr('data-name');
  //onsole.log(inventory);
  var SalesPrice = $('.Product option:selected').attr('data-price');
  var CostPrice = $('.Product option:selected').attr('cost-price');
  var description = $('.Product option:selected').attr('data-pro');
  var total=qty*SalesPrice;

  var tr = '<tr id="row">' + 

  '<td><input type="text" class="inventId form-control"  value='+inventory+' name="inventId[]" readonly /></td>'+

  '<td > <input type="number" class="qty form-control"  value='+qty+' name="qty[]" > </td>' +

  '<td style="display: none;"><input type="text" class="costprice form-control" name="costprice[]"  value='+CostPrice+' readonly></td>' +

   '<td><input type="text" class="price form-control" name="price[]"   value='+SalesPrice+' readonly></td>' +

   '<td><input type="text" class="amount form-control" name="amount[]"  value='+total+' readonly></td>' +

   '<td><span class="fa fa-trash delete" data-toggle="tooltip" data-original-title="Remove Item" value="x" style="margin-left: 36px;margin-top: 14px;"></span></td>' +
    '</tr>'; 
  $('.neworderbody').append(tr);

});