【问题标题】:I want to clear the fields after jquery clone of table rows我想在 jquery 克隆表行后清除字段
【发布时间】:2020-07-24 23:39:43
【问题描述】:

我使用下面的 jquery 代码为我的简单发票应用程序克隆表行: 我想要的是当 tr 被克隆时,2 个输入字段应该是空的价格和数量,我将非常感谢您的帮助,在此先感谢:-

jQuery 代码:


function autoCalcSetup() {
    $('form[name=cart]').jAutoCalc('destroy');
    $('form[name=cart] tr[name=line_items]').jAutoCalc({keyEventsFire: true, decimalPlaces: 2, emptyAsZero: true});
    $('form[name=cart]').jAutoCalc({decimalPlaces: 2});
}
autoCalcSetup();
$('button[name=remove]').click(function(e) {
    e.preventDefault();
    var form = $(this).parents('form')
    $(this).parents('tr').remove();
    autoCalcSetup();
});


$('button[name=add]').click(function(e) {
    e.preventDefault();
    var $table = $(this).parents('table');
    var $top = $table.find('tr[name=line_items]').first();
    var $bottom = $table.find('tr[name=endin_colz]').first();
    var $new = $top.clone(true);
    $new.jAutoCalc('destroy');
    $new.insertBefore($bottom);
    autoCalcSetup();

});

<tr name="line_items">
  <td><button name="remove" class="btn btn-danger btn-sn">Remove</button></td>
  <td>
      <select class="form-control" name="productname[]" style="margin-bottom: 5px;">
               <option>Select...</option>
               <option>Barcode Machine</option>
<option>Snap frame A4</option>
<option>Snap frame A3</option>
<option>Snap frame A2</option>
<option>Snap frame A1</option>
<option>Snap frame A0</option>
<option>Gazebo Aluminum Foldable</option>
<option>Key Cabinet 24 keys</option>
<option>Vinyl Roll</option>
               </select>

       <textarea class="form-control" rows="5" name="particulars[]" ></textarea></td>

  <td><input type="text" id="qty" name="qty[]" class="form-control form-control-sn qty"></td>
  <td><input type="text" id="price" name="price[]" class="form-control form-control-sn price"></td>

  <td><input type="text" name="item_total[]" jAutoCalc="{#qty} * {#price}" class="form-control itemtotal" readonly></td>

  <td><input type="hidden" name="invidfk[]" value="<?php echo $inv_code; ?>"class="form-control form-control-sm"></td>
  </tr>

【问题讨论】:

  • 应该为空的字段的 id 如下:- 2 个输入,id 为 qty,克隆后价格应该是空的,因为它是从表的准备标记中克隆的。

标签: php jquery clone rows


【解决方案1】:

您可以使用$new.find("input").val(""); 将两个输入字段都设置为空

完整代码:

$('button[name=add]').click(function(e) {
    e.preventDefault();
    var $table = $(this).parents('table');
    var $top = $table.find('tr[name=line_items]').first();
    var $bottom = $table.find('tr[name=endin_colz]').first();
    var $new = $top.clone(true);
    $new.jAutoCalc('destroy');
    $new.find("input").val("");
    $new.insertBefore($bottom);
    autoCalcSetup();    
});

但请注意,您的代码存在一个普遍问题 - 如果您克隆具有 id 的元素并添加它们,您的 HTML 将不再有效,因为 id 必须是唯一的。最好改用类。

【讨论】:

  • 感谢 matthias_h 的快速响应,我已经添加了 html 代码,以便您可以更好地了解正在克隆的内容,其名称为 line_items 的整个 tr 在克隆时被克隆应该只清空新克隆的数量和价格,你可以看到我也在使用 autocalc 库进行计算。
  • 谢谢它的工作我按照你的解决方案并更改为类名!!!很好的反应我的朋友!我也感谢整个社区
  • @gratitudetags 很高兴我能提供帮助 :) 如果这回答了您的问题,您可以接受此答案,这样您的问题将被标记为已关闭/已解决。如果您不知道这是如何工作的,请查看meta.stackexchange.com/questions/5234/…
  • 对不起,这些值仍然被克隆,添加的行并没有真正做到这一点,对不起,任何更多的帮助将得到极大的应用......
  • @gratitudetags 我制作了这个小提琴jsfiddle.net/t5x2wgjL,其中我注释掉了自动计算函数调用,因为没有库它们将无法工作。当您删除 $new.jAutoCalc('destroy'); 行时,您能否检查它是否适用于您? ?
猜你喜欢
  • 2011-10-16
  • 1970-01-01
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多