gpt4 book ai didi

javascript - 动态添加选择框

转载 作者:行者123 更新时间:2023-12-02 18:54:19 26 4
gpt4 key购买 nike

我正在使用 JSP 创建一个应用程序。在其中一页中,用户需要输入一些症状。现在我想给一个按钮。当用户单击此按钮时,将创建一个新的选择框。

我搜索了很多,只能找到如何动态添加选项来选择,但我想要新的选择框

请有人帮忙。

最佳答案

这是一个示例代码:

<html>
<head>
<script type="text/javascript">
function addSelectBox ()
{
var parentDiv = document.getElementById ("main");
var selectElement = document.createElement ("select");
for (var i=0;i < 5;i++)
{
var option = new Option ("Text " + i, "Value" + i);
selectElement.options[selectElement.options.length] = option;
}
parentDiv.appendChild (selectElement);
}
</script>
</head>
<body>
<div id="main">
<input type="button" onclick="addSelectBox ()" name="clickme" value="Add Select Box" />
</div>
</body>
</html>

编辑:

对于 JSP(如果 ArrayList 中有选项):

    <html>
<head>
<%
int totalElements = 5;
ArrayList r = new ArrayList (totalElements);
for (int i=1;i <= totalElements;i++)
{
r.add (String.valueOf (i));
}
%>
<script type="text/javascript">
function addSelectBox ()
{
var parentDiv = document.getElementById ("main");
var selectElement = document.createElement ("select");
var option;
<%
for (int i=0;i <= r.size();i++)
{
%>
option = new Option ('<%=r.get (i)%>', '<%=r.get (i)%>');
selectElement.options[selectElement.options.length] = option;
<%
}
%>
parentDiv.appendChild (selectElement);
}
</script>
</head>
<body>
<div id="main">
<input type="button" onclick="addSelectBox ()" name="clickme" value="Add Select Box" />
</div>
</body>
</html>

关于javascript - 动态添加选择框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15530110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com