2017-08-29 118 views
0

我使用输入框创建表并使用以下代码创建新行。通过javascript更改类输入值

我希望新行在文本框内应该有空白值。对于它我已经尝试过类属性改变,但它不工作,总是从第一行获得价值。

function addRow(tableID) { 
    var table = document.getElementById(tableID); 
    var rowCount = table.rows.length; 

          // limit the user from creating fields more than your limits 
     var row = table.insertRow(rowCount); 
     var colCount = table.rows[0].cells.length; 
     for(var i=0; i<colCount; i++) { 
      var newcell = row.insertCell(i); 
      newcell.innerHTML = table.rows[1].cells[i].innerHTML; 
       document.getElementsByClassName('small')[0].setAttribute("value", ""); 
    } 
} 
+0

代码搜索'document'的所有元素。你也可以在特定的元素上使用'getElementsByClassNamedocument'。未经测试,但您可以尝试'newcell.getElementsByClassName(')而不是(尽管更好的方式是在循环后用'row.getElementsByClassName'循环所有元素) –

回答

0

你只是像这样设置你的第一行的值: document.getElementsByClassName('small')[0].setAttribute("value", "");

你通过他们都需要循环并将其值设置。

document.getElementsByClassName('small'),forEach(item => item.setAttribute('value','');

+0

将此代码放入代码后无输出 –

0

value属性不正是你所需要的,并在所有可编写脚本的浏览器正常工作。

var input = document.getElementById("your_input"); 
input.value = "blah"; 

在你的情况,你可以试戴

document.getElementsByClassName('small')[0].value = ""; 
+0

对此结果相同 –