它似乎很简单:我有这个表格与Javascript代码。 “ - ”按钮删除一个输入集,“+”按钮(在代码底部)应该添加一个,但它不起作用? 脚本最初来自http://www.quirksmode.org/dom/domform.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Formular</title>
<script type="text/javascript">
<!--
function init() {
document.getElementById('moreFields').onclick = moreFields;
moreFields();
}
var counter = 0;
function moreFields() {
counter++;
var newFields = document.getElementById('readroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
window.onload = moreFields;
-->
</script>
</head>
<body>
<a href="/">Home</a> | <a href="/new/">Neue Regel</a><br><hr><br>
<div id="readroot" style="display: none">
<p>Adresse: <input name="c_adddress" type="text">
Value: <input name="c_value" type="text">
<input type="button" value=" - " onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /></p></div>
<form action="/save/">
<p>Name:<input name="name" type="text"></p>
<p><b>Befehl:</b><br>
<p>Adresse: <input name="b_adddress" type="text">
Value: <input name="b_value" type="text"></p>
<p><b>Bedingungen</b></p>
<span id="writeroot"></span>
<input type="button"id="moreFields" value=" + " />
<p><input type="submit" value="speichern">
<input type="reset" value="zurücksetzen"></p>
</form>
</body>
</html>
答案 0 :(得分:2)
您的moreFields
按钮未注册onclick事件。你还没有在任何地方调用init
函数。
更改以下行:
document.getElementById('moreFields').onclick = moreFields; // Notice no ()
...
window.onload = init; // This will make sure the above code is run after the DOM is loaded