我希望用戶單擊按鈕並出現表單。但不管他們按了多少次這個按鈕,表格都會出現。按照它們創建的順序依次排列。我想在JavaScript中實現。在onclick上動態添加輸入元素
我不是專業人士,我正在自學。只是想知道我是否可以說:
<button id="new"><span class="glyphicon glyphicon-plus"> Add New Add-on</span></button>
這可能嗎?用我讀過的,我想出了這個。但正如我所預料的那樣,它不起作用。
<input type="button" id="new" name="new" value="Add New Add-On" class="btn" onclick="addFields()">
<div id="New_Form"/>
</div>
<script>
function addFields(){
var button = document.getElementById("new").value;
var container = document.getElementById("New_Form");
while (New_Form.hasChildNodes()) {
New_Form.removeChild(New_Form.lastChild);
}
for (i=0;i<number;i++){
New_Form.appendChild(document.createTextNode("new " + (i+1)));
var input = document.createElement("input");
input.type = "button";
New_Form.appendChild(input);
New_Form.appendChild(document.createElement("br"));
}
}
</script>
爲什麼jQuery標籤? – j08691
「並出現一個表單」,但您的代碼添加了輸入,而不是「表單」,請通過重新編輯或編輯問題來澄清。 –