2013-04-22 20 views
0

我正在使用以下代碼來使用JavaScript製作html5表單。使用JavaScript製作HTML表單時不能使用新行

newLine = document.createElement("br"); 
var optionsForm = document.createElement("form"); 
optionsForm.setAttribute('method',"post"); 
optionsForm.setAttribute('action',"submit.php"); 
var selectColor = document.createElement("select"); //input element, drop-down 
selectColor.setAttribute('type',"select"); 
selectColor.setAttribute('name',"colorMenu"); 
selectColor.setAttribute('id',"colorMenuID"); 

// Populate drop-down menu 
var option = document.createElement("option"); 
option.text = "Select Color:"; 
option.value = "selectColor"; 
selectColor.appendChild(option); 

optionsForm.appendChild(selectColor); 
optionsForm.appendChild(newLine); 

但是,當我創建新的下拉菜單時,它與舊的菜單位於同一行。附加newLine元素似乎沒有任何作用。

+0

u能看到
標籤出現在生成的HTML代碼?嘗試使用螢火蟲來檢查它 – dreamweiver 2013-04-22 13:23:53

+0

你試過用\ n嗎?或nl2br(PHP的換行符) – 2013-04-22 13:25:01

+0

你不想在''optionsForm.appendChild(selectColor)''之前運行'optionsForm.appendChild(newLine);'**? – Ian 2013-04-22 13:37:13

回答

0

我想出瞭如何做到這一點。

var menuElement = document.createElement("div"); 
label = document.createTextNode("Label: "); 
menuElement .appendChild(label); 
var selectMenu = document.createElement("select"); 
selectMenu.setAttribute('type',"select"); 
selectMenu.setAttribute('name',"menuName"); 
selectMenu.setAttribute('id',"menuID"); 
menuElement.appendChild(selectMenu); 

然後,添加一些選項,然後

menuElement.appendChild(newLine); 
optionsForm.appendChild(menuElement);