【发布时间】:2018-07-29 22:27:55
【问题描述】:
目前我有这个设置。 我的页面加载了 2 个相邻对齐的下拉菜单和一个添加按钮。 ADD 按钮使用函数 addRow() 在下一行添加一个下拉列表。 该函数可能是最糟糕的实现。
a) 我希望添加按钮将 2 个类似的下拉菜单添加到下一行,这些下拉菜单与最初在页面中相邻对齐。 (现在代码只在下一行添加了 1 个下拉菜单)
b) 有什么方法可以将添加按钮放在我拥有的下拉菜单下方,而不是放在第一行下拉菜单旁边?
下面是代码
<div class="container">
<table>
<tr>
<td>
<select id="soflow">
<!-- This method is nice because it doesn't require extra div tags, but it also doesn't retain the style across all browsers. -->
<option>Select an Option</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
<select id="soflow">
<!-- This method is nice because it doesn't require extra div tags, but it also doesn't retain the style across all browsers. -->
<option>Select an Option</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
<button class = "button" type="button" onClick ="addRow(this)">Add</button></td>
</tr>
</table>
</div>
<script>
function addRow(btn) {
var parentRow = btn.parentNode.parentNode;
var table = parentRow.parentNode;
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
//var element2 = document.createElement("input");
var element2 = document.createElement("select");
element2.setAttribute("id", "soflow")
//element2.type = "select";
var option1 = document.createElement("option");
option1.innerHTML = "Option1";
option1.value = "1";
element2.add(option1, null);
var option2 = document.createElement("option");
option2.innerHTML = "Option2";
option2.value = "2";
element2.add(option2, null);
cell1.appendChild(element2);
}
</script>
感谢您的帮助。
【问题讨论】:
标签: javascript html