簡體   English   中英

我的選擇列表中的所有選項都在一個大選項中連接在一起。 我該如何解決?

[英]All the options in my select list are joined together in one big option. How can I fix it?

這是我第一次做表單處理DOM節點而不是經典的HTML代碼,有些東西讓我感到困惑。 我有一個包含多個選項的select列表,但不是將每個選項放在一個新行中,而是在第一個選項中加入所有選項。

我很確定我錯過了一些非常簡單的東西,但我看不清楚。 拜托,你能幫我看看我做錯了什么嗎? 謝謝! 這是代碼:

var divcontainer = document.createElement("div");
var formulario = document.createElement("form");

var selectgrup = document.createElement("select");
var optiongrup = document.createElement("option");

var opcion = document.createTextNode("The first option");
optiongrup.appendChild(opcion);
selectgrup.appendChild(optiongrup);

var opcion = document.createTextNode("The second option");
optiongrup.appendChild(opcion);
selectgrup.appendChild(optiongrup);

formulario.appendChild(selectgrup);

divcontainer.appendChild(formulario);

document.body.appendChild(divcontainer);​

看看會發生什么: JSFiddle

您需要為第二個項目創建一個新的“選項”元素

optiongrup = document.createElement("option");
var opcion = document.createTextNode("The second option");
optiongrup.appendChild(opcion);
selectgrup.appendChild(optiongrup);

檢查我的小提琴: http//jsfiddle.net/BuddhiP/q5WLX/2/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM