我是JavaScript DOM的新手。我在www.quirksmode.org/dom/domform.html找到了一個教程。我重寫了代碼以滿足我的需求,但是當我無法使其工作時,我將此頁面中的代碼直接複製到測試頁面中。 moreFields函數將運行onload,但不是onclick。 removeFields函數確實有效。我已經在Firefox和Chromium中測試了代碼。它在教程中的Firefox中工作,但即使我逐字複製它也不行。我甚至嘗試從教程中複製源代碼。如何使用Javascript無限期地將字段添加到表單中?
<script type="text/javascript">
<!--
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>
<div id="readroot" style="display: none">
<input type="button" value="Remove review"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br /><br />
<input name="cd" value="title" />
<select name="rankingsel">
<option>Rating</option>
<option value="excellent">Excellent</option>
<option value="good">Good</option>
<option value="ok">OK</option>
<option value="poor">Poor</option>
<option value="bad">Bad</option>
</select><br /><br />
<textarea rows="5" cols="20" name="review">Short review</textarea>
<br />Radio buttons included to test them in Explorer:<br />
<input type="radio" name="something" value="test1" />Test 1<br />
<input type="radio" name="something" value="test2" />Test 2
</div>
<form method="post" action="/cgi-bin/show_params.cgi">
<span id="writeroot"></span>
<input type="button" id="moreFields" value="Give me more fields!" />
<input type="submit" value="Send form" />
</form>
我忘了提,我在一個PHP變量輸入這讓我有逃離「'」 S –
你可以在javascript中使用「樣式引用」,如果這些引用不需要轉義,它會使整個事物變得更具可讀性 – dspeyer
你的腳本有語法問題,你不應該使用'\''ex '\'readroot \''應該是''readroot''或''readroot'' –