日期:2014-05-16  浏览次数:20349 次

动态删除文本框问题
添加文本框和删除文本框函数代码
<script   type= "text/javascript ">
<!--
//   在一个元素里添加文本框
function   addTextBox(InsertElement)  
{
var   textField   =   document.createElement( "input ");
textField.setAttribute( "type ", "text ");
textField.setAttribute( "name ",InsertElement);
textField.setAttribute( "size ",   50);
document.getElementById(InsertElement).appendChild(textField);

return   true;
}

function   removeTextBox(DeleteElement)  
{
var   oDelTextBox   =   document.getElementById(DeleteElement).lastChild;
oDelTextBox.parentNode.removeChild(oDelTextBox);
}
//-->
</script>

需要动态添加文本框的地方代码
<tr>
<td   align   =   center> 顶板&nbsp; <input   type   =   button   value   =   添加   onclick   =   addTextBox( "Board ")> &nbsp; <input   type   =   button   value   =   删除   onclick   =   removeTextBox( "Board ")>
</td>
<td   id   =   Board> &nbsp; </td>
</tr>

问题:现在添加文本框没有问题,但删除文本框时,第一次按按钮是不起作用的,再按一次才能删除一个文本框,要想再删除一个,又需要按两次删除按钮,高手们哪里出了问题了?

------解决方案--------------------
问题出在parentNode这,代码中在要删除的input框和前面的部分不要有换行,有换行了第一次删除的换行,所以第一次没用