我正在尝试通过单击按钮来动态添加和删除文本框。add()
有效,但无效remove()
。
$ctra = 1;
function add() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = "<table><tr><td width=159> Name</td>"+
"<td> <input type='text' name='name' size=20> </td>"+
"<td> Address </td>"+
"<td> <input type='text' name='address' size=20> </td>"+
"<td> Tel. No </td>"+
"<td> <input type='text' name='telno' size=20><td> <button type='button' onclick='remove(divIdName);'>Remove</button></table>";
if($ctra<5){
ni.appendChild(newdiv);
$ctra++;
}
$total = $ctra;
}
这是remove()
. 它接受div
.
function remove(dId) {
var ni = document.getElementById('myDiv');
ni.removeChild(dId);
}
请帮我解决问题。谢谢!