非常新的Java,有一个用Thymeleaf和Spring创建的表单。我需要一种方法来插入新的输入文本字段,如果一个按钮被点击。你知道怎么做到这一点吗?我想我需要在@RequestMapping/Controller文件中做点什么。但是,我如何生成一个新的输入行,我需要在模型中定义一些额外的东西吗?这是按钮..。
<!-- 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>
发布于 2018-09-20 05:59:46
为此,您需要使用Javascript,并在前端完成。
在使用原始javascript的示例中,首先将id添加到父元素,并为按钮提供一个click处理程序:
<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函数
<script>
function addInputLine() {
var node = document.createElement("input"); // Create an <input> node
document.getElementById("parentElement").appendChild(node); // Append it to the parent
}
</script>
从长远来看,您最好使用jquery这样的框架来处理这类事情
https://stackoverflow.com/questions/52414590
复制相似问题