首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在单击Spring/Java/Thymeleaf按钮时添加新的输入字段

如何在单击Spring/Java/Thymeleaf按钮时添加新的输入字段
EN

Stack Overflow用户
提问于 2018-09-20 05:45:01
回答 1查看 989关注 0票数 0

非常新的Java,有一个用Thymeleaf和Spring创建的表单。我需要一种方法来插入新的输入文本字段,如果一个按钮被点击。你知道怎么做到这一点吗?我想我需要在@RequestMapping/Controller文件中做点什么。但是,我如何生成一个新的输入行,我需要在模型中定义一些额外的东西吗?这是按钮..。

代码语言:javascript
复制
        <!-- add option for user to add another input line -->
        <div class="row text-right">
            <label>Add another input line</label>
            <button type="submit" name="addInputLine" class="btn btn-default" ><span class="fa fa-plus"></span></button>
        </div>

EN

回答 1

Stack Overflow用户

发布于 2018-09-20 05:59:46

为此,您需要使用Javascript,并在前端完成。

在使用原始javascript的示例中,首先将id添加到父元素,并为按钮提供一个click处理程序:

代码语言:javascript
复制
<div id="parentElement" class="row text-right">
    <label>Add another input line</label>
    <button onclick="addInputLine()" name="addInputLine" class="btn btn-default" ><span class="fa fa-plus"></span></button>
</div>

接下来,为click处理程序添加javascript函数

代码语言:javascript
复制
<script>
function addInputLine() {
var node = document.createElement("input");                 // Create an <input> node                         
document.getElementById("parentElement").appendChild(node);     // Append it to the parent
}
</script>

从长远来看,您最好使用jquery这样的框架来处理这类事情

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52414590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档