我試圖創建一個按鈕以將文本字段添加到窗體。我有幾個問題。首先,當我按下添加選擇按鈕時,它不會將新文本框放在最後選擇下,而是放在旁邊。另外,我不知道如何在文本框之前添加文本「Chocie:」。任何幫助?這是我的代碼。嘗試使用javascript將字段添加到字段中
創建一個新的投票
<form id="myForm" action="/polls/" method="post">
{% csrf_token %}
Question: <input type="text" name="poll"><br />
<div id="Choices">
Choice: <input type="text" name="choice1"><br />
Choice: <input type="text" name="choice2"><br />
Choice: <input type="text" name="choice3"><br />
Choice: <input type="text" name="choice4"><br />
Choice: <input type="text" name="choice5"><br />
</div>
<a href="javascript:addOption();">Add option</a> <br />
<input type="submit" value="Submit" />
</form>
<script>
var choiceNumber = 6; //The first option to be added is number 6
function addOption() {
var choices = document.getElementById("Choices");
var newChoice = document.createElement("input");
newChoice.name = "choice"+choiceNumber;
newChoice.type = "text";
choices.appendChild(newChoice);
choiceNumber++;
}
</script>
我在段落元素中放什麼? – 2012-02-17 01:54:31