【发布时间】:2014-12-10 00:31:40
【问题描述】:
我有这个代码
var form = document.getElementById("form");
var thirdElement = document.createElement("select");
var thirdElementOp1 = document.createElement("option");
thirdElementOp1.value = "--IDEAS--";
var thirdElementOp2 = document.createElement("option");
thirdElementOp2.value = "Random";
var thirdElementOp3 = document.createElement("option");
thirdElementOp3.value = "--FUN--";
var thirdElementOp4 = document.createElement("option");
thirdElementOp4.value = "Reflective";
thirdElement.appendChild(thirdElementOp1);
thirdElement.appendChild(thirdElementOp2);
thirdElement.appendChild(thirdElementOp3);
thirdElement.appendChild(thirdElementOp4);
form.appendChild(thirdElement);
这会将所有这些子元素附加到一个表单中,生成以下 HTML 代码:
<form id="form" action="home.php" method="post">
<select>
<option value="TheJSGeneratedValue"></option>
<option value="TheJSGeneratedValue"></option>
<option value="TheJSGeneratedValue"></option>
<option value="TheJSGeneratedValue"></option>
</select>
</form>
我的问题是,要让这些值出现在下拉菜单中,我必须像这样在标签内写:
<option value="TheJSGeneratedValue">TheJSGeneratedValue</option>
有什么方法可以在 JavaScript 上做到这一点吗?我接受 jQuery 答案,但我会很感激 JavaScript 答案,我猜这可能与 thirdElementOp1.valueInside = "Value" 或类似的东西有关......
【问题讨论】:
标签: javascript html forms tags appendchild