【发布时间】:2011-08-23 03:53:07
【问题描述】:
我正在尝试创建一个动态表单,当单击按钮时,它会在某个表格中添加元素。为此,我调用了一个 javascript 函数,在本例中,该函数将创建一个新的文本框,该文本框的名称和类型为“item_3”,旁边有删除链接。该网页在 Firefox 和 chrome 中运行良好,可能是因为它们没有像 Internet Explorer 那样严格使用 W3C。它不会在 IE8 中运行,并会给出错误“Unknown Runtime Error”。我确信这个问题与 DOM 以及我如何使用我的元素有关,但我需要一种在特定表中动态添加元素的方法。这是头:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script language="Javascript" type="text/javascript">
//Add more fields dynamically.
function addField(area,type)
{
if(!document.getElementById) return; //Prevent older browsers from getting any further.
var field_area = document.getElementById(area); //Select table to insert element
//Example, i want a textbox with id and name as item_3
var type = "item_";
var count = "3";
//Add html to insertiontable table
field_area.innerHTML += "<tr><td colspan='2'><input type='text' name='"+(type+count)+"' id='"+(type+count)+"'/><a style='cursor:pointer;color:blue;' onclick='this.parentNode.parentNode.removeChild(this.parentNode);'> Remove Box</a></td</tr>";
}
</script>
</head>
正文部分包含我的表单。有两个表,table1 和插入表。表一包含一个文本框和一个按钮,用于将新的“item_3”元素插入到插入表中。
<body>
<form name="newbookform1" method="post" action ="">
<!--Table one-->
<table id="table1" align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td><strong>What:</strong></td><td><input type="text" name="item_1" id="item_1" /></td><td><input type="button" value="Add New Item Type" onclick="addField('insertiontable','item_');" /></td></tr>
</table>
<!--Table two-->
<table id="insertiontable" align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td colspan="2"><input type="text" name="item_2" id="item_2" /><a style="cursor:pointer;color:blue;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"> Remove Box</a></td></tr>
</table>
</form>
</body>
</html>
清理这个让我很头疼,这如何在 IE8 中使用有效的 HTML 工作?非常感谢您的帮助。
【问题讨论】:
标签: javascript html forms internet-explorer