如何在按钮上添加HTML并将对象或其字段绑定到该HTML的html?
这是一个简单的Java类,例如:html带有胸腺的html列表
public class Apple {
private List<String> listOfStrings;
// getters setters
}
:
<form method="post" th:object="${apple}"> // apple is set through Model.addAttribute(.., ..) in Controller
<input type="text" th:each="str, iter : *{listOfStrings} th:field="*{listOfStrings[__${iter.index}__]}">
<button id="add" type="button" onclick="add()>Add</button>
<button id="remove" type="button" onclick="remove()">Remove</button>
<input type="submit" value="Save">
<form>
javascript添加和删除新输入:
const newInputTextString="<input type=\"text\" th:field="?">" // th:field won't be evaluated/bound to apples listOfStrings index
function add() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("add").insertAdjacentHTML("beforebegin", newInputTextString);
}
xhttp.open("GET", window.location.href);
xhttp.send();
}
function remove() {
// TODO
}
到目前为止,我只通过JavaScript添加HTML,但是我不能在其中包含TH:因为这些标签不会评估这些标签并绑定到特定对象或字段。
预期功能:
- 单击“删除”按钮,删除了与ListOfstrings特定索引的类型文本的最后输入。
- 单击“添加”按钮,添加了类型文本的新输入并将其绑定到ListOfstrings的下一个索引。
换句话说,我想让用户能够使用HTML按钮在该Apple对象的ListOfstrings中删除并添加字符串。
随意纠正我的英语
This is simple java class that has a list of strings for example:
public class Apple {
private List<String> listOfStrings;
// getters setters
}
html with thymeleaf:
<form method="post" th:object="${apple}"> // apple is set through Model.addAttribute(.., ..) in Controller
<input type="text" th:each="str, iter : *{listOfStrings} th:field="*{listOfStrings[__${iter.index}__]}">
<button id="add" type="button" onclick="add()>Add</button>
<button id="remove" type="button" onclick="remove()">Remove</button>
<input type="submit" value="Save">
<form>
javascript add and remove new input:
const newInputTextString="<input type=\"text\" th:field="?">" // th:field won't be evaluated/bound to apples listOfStrings index
function add() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("add").insertAdjacentHTML("beforebegin", newInputTextString);
}
xhttp.open("GET", window.location.href);
xhttp.send();
}
function remove() {
// TODO
}
So far I am only adding html through javascript, but I can not include th: tags there since these tags wont be evaluated and bind to specific object or fields.
Expected functionality:
- on click of remove button, last input of type text that is bound to specific index of listOfStrings is removed.
- on click of add button, new input of type text is added and bound to next index of listOfStrings.
In other words I want to make user able to remove and add strings in listOfStrings of that apple object with html buttons.
Feel free to correct my English
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TH:字段本质上只是在元素上为您提供一个ID和名称属性。您不能通过JS添加一个字段,但是可以通过手动添加适当的ID和名称属性来模仿其行为。话虽如此,您应该对HTML和JS代码进行一些修改。
您应该始终拥有当前字符串总数的记录,因此您可以调整字符串索引。为此,我围绕所有字符串输入创建了一个DIV包装器,因此我们可以在JS中访问它。之后,我们只是阅读其中的元素数量,并从中创建一个新的索引。
您可以查看以下实现:
th:field essentially just gives you an ID and a NAME attribute on an element. You cannot add a th:field through JS but you can emulate its behavior by manually adding the appropriate ID and NAME attributes. With that said you should make some slight changes in your HTML and JS code.
You should always have a record of the current total number of strings so you can adjust your string index. For that I've created a div wrapper around all string inputs so we can access it in JS. Afterwards we just read the number of elements inside of it and create a new index out of it.
You can look the implementation below: