用户点击按钮时动态添加行?

时间:2016-09-23 07:18:06

标签: javascript html

用户点击按钮时动态添加行。我制作了一个剧本,但它不起作用PLZ帮帮我

   
<script>
var t;
t=2;
function Insert(){
var tab=document.getElementById("mytable");

var row=insertRow(t);
var cell1=insertCell(0);
var cell2=insertCell(1);
var cell3=insertCell(2);
var cell4=insertCell(3);
 
t=t+1;
 
 
   cell2.setAttribute('contenteditable','true');
   cell3.setAttribute('contenteditable','true');
   cell4.setAttribute('contenteditable','true');
}
</script>
Html 
<input type="button"  onclick="Insert()" name="Insert Row" value="Insert Row" />    

2 个答案:

答案 0 :(得分:2)

function addRow(tableID) {

			var table = document.getElementById(tableID);

			var rowCount = table.rows.length;
			var row = table.insertRow(rowCount);


			var cell3 = row.insertCell(0);
			var element2 = document.createElement("input");
			element2.type = "text";
			element2.name = "txtbox[]";
			cell3.appendChild(element2);


		}
<BODY>

	<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />


	<TABLE id="dataTable" width="100px" border="1">
		<TR>
			<TD> <INPUT type="text" /> </TD>
		</TR>
	</TABLE>

</BODY>

答案 1 :(得分:1)

更改您的脚本

&#13;
&#13;
<script>
var t;
t=2;
function Insert(){
var tab=document.getElementById("mytable");

var row=tab.insertRow(t);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
var cell4=row.insertCell(3);
 
t=t+1;
 
 
   cell2.setAttribute('contenteditable','true');
   cell3.setAttribute('contenteditable','true');
   cell4.setAttribute('contenteditable','true');
}
</script>
&#13;
&#13;
&#13;