我想要一个表和一个文本框,当我为文本框输入一些值然后单击按钮时,该值将被添加到表中。我编写了代码,但它没有运行。请帮我找出下面代码有什么问题。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
function send() {
tbl=document.getElementById("idtable");
tr = document.createElement("tr");
td1 = document.createElement("td");
td2 = document.createElement("td");
name = document.getElementById("txtname").value;
email = document.getElementById("txtemail").value;
td1.innerText=name;
td2.innerText=email;
tr.appendChild(td1);
tr.appendChild(td2);
tbl.appendChild(tr);
}
</script>
</head>
<body>
<input type="text" id="txtname" name="txtname"/> <br/><br/>
<input type="text" id="txtemail" name="txtemail"/>
<p> <input type="button" value="Send" onclick="send()"/>
<table id="idtable" border="1">
<tr><td>Name</td>
<td>Email</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:1)
试试这段代码:
function send(){
var table=document.getElementById("table");
name=document.getElementById("txtname").value;
email=document.getElementById("txtemail").value;
table.innerHTML+=('<tr><td>'+name+'</td><td>'+email+'</td></tr>')
}
&#13;
<input id="txtname" name="txtname"/> <br/>
<input id="txtemail" name="txtemail"/> <br/>
<input type="button" value="Send" onclick="send()"/><br/>
<table id="table" border="1">
<tr><td>Name</td><td>Email</td></tr>
</table>
&#13;