JavaScript克隆表行 - 重置值

时间:2015-12-16 18:34:17

标签: javascript forms html-table

我有一个表单/表设置,它克隆具有特定值名称的行,以便在表单提交后可以单独处理它们。除非您填写第5行(正在克隆的行)的信息,然后将所有数据复制到克隆的行中,否则一切似乎都很顺利。

在克隆过程中,是否有一种简单的方法可以将值重置为NULL和0?

(我在底部有两行记录/总计每列,因此tr:nth-last-child(2)

var totalRows = 5;

function addrow() {
  totalRows++;
  var clonedRow = $("table#maintable tr:nth-child(5)").clone(); //this will grab the 5th table row.

  //get the textfield
  var tfName = $("input.name",clonedRow);
  var tfproA = $("input.proA",clonedRow);
  var tfproB = $("input.proB",clonedRow);
  var tfproC = $("input.proC",clonedRow);
  var tfproD = $("input.proD",clonedRow);
  var tfproE = $("input.proE",clonedRow);
  var tfproF = $("input.proF",clonedRow);
  var tfproG = $("input.proG",clonedRow);
  var tfproH = $("input.proH",clonedRow);
  var tfproI = $("input.proI",clonedRow);
  var tfproJ = $("input.proJ",clonedRow);
  var tfproK = $("input.proK",clonedRow);
  var tfproL = $("input.proL",clonedRow);
  var tfproM = $("input.proM",clonedRow);
  var tfproN = $("input.proN",clonedRow);

  //change their names
  tfName.attr("name","name"+totalRows);
  tfproA.attr("name","proA"+totalRows);
  tfproB.attr("name","proB"+totalRows);
  tfproC.attr("name","proC"+totalRows);
  tfproD.attr("name","proD"+totalRows);
  tfproE.attr("name","proE"+totalRows);
  tfproF.attr("name","proF"+totalRows);
  tfproG.attr("name","proG"+totalRows);
  tfproH.attr("name","proH"+totalRows);
  tfproI.attr("name","proI"+totalRows);
  tfproJ.attr("name","proJ"+totalRows);
  tfproK.attr("name","proK"+totalRows);
  tfproL.attr("name","proL"+totalRows);
  tfproM.attr("name","proM"+totalRows);
  tfproN.attr("name","proN"+totalRows);

  $("table#maintable tr:nth-last-child(2)").before(clonedRow); //add the row back to the table before the last 2nd to last row
  console.log('done')
}

1 个答案:

答案 0 :(得分:1)

您可以将对val()

的调用链接在一起

tfName.attr(“name”,“name”+ totalRows).val(0)或

tfName.attr( “姓名”, “姓名” + totalRows).VAL( '')