我想创建一个动态表单,通过点击按钮(或图像)向表单中添加输入项,在onclick事件中直接放置代码即可实现。
alert("Hello");
它可以正常工作...但是,当我尝试调用JavaScript函数时,它无法工作...即使我调用简单的 hello() 函数(也适用于 add() 函数)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function add() {
var container = document.createElement("div");
container.innerHTML = "<input type='text' name='email'><br/>";
document.getElementById('emails').appendChild(container);
}
function hello() {
alert("Hello");
}
</script>
</head>
<body>
<form id="myform" name="myform" action="valid.php" method="POST">
<div id="emails">
<input type='text' name='email' value=''/><br />
</div>
<img width="50px" src="myimage.jpg" onclick='add();'/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
有人能发现为什么吗?
hello
指定为onclick事件处理程序。 - Viktor S.