我的表单中有两个按钮(第一个和第二个)。现在,我想在单击第一个按钮时更新第二个按钮的禁用功能。
<form action="updateButton" method="post" id="something">
<h1 th:text="${application.text}"></h1>
<button type="submit" name="first">First</button>
<button type="submit" name="second" th:disabled="${model.disabled}">Second</button>
</form>
我的 Controller :
private Boolean disabled = false;
public Boolean getDisabled() {
return disabled;
}
public void setDisabled(Boolean disabled) {
this.disabled = disabled;
}
有没有办法将 boolean 变量与我的第二个按钮绑定(bind),以便该按钮启用/禁用第一个按钮的 onclick?我知道我必须用 PostMapping("updateButton") 编写一个方法。但我不知道如何绑定(bind)变量。
最佳答案
此实现应使用 Javascript
而不是 Thymeleaf
完成。
使用 Thymeleaf 进行此操作并重新加载页面是没有意义的。
但是如果您对此太感兴趣,您可以使用以下内容向表单添加操作网址:
<form action="updateButton" method="get" id="something" action="/ws">
<h1 th:text="${application.text}"></h1>
<button type="submit" name="first">First</button>
<button type="submit" name="second" th:disabled="${model.disabled}">Second</button>
</form>
那么您的 Controller 中需要一个 Web 服务来处理这种情况并重定向到映射 View
,将属性设置为 true。
@RequestMapping(value = "/ws")
public ModelAndView disable(@RequestParam("second") String second, Model model) {
Model model = new Model();
model.addAttribute("disableSecondButton", true);
return "yourview";
}
最后添加条件:
<div th:switch="${disableSecondButton}">
<button type="submit" name="second" th:case="'true'" disabled>Second</button>
<button type="submit" name="second" th:case="*">Second</button>
</div>
关于java - Spring MVC 和 Thymeleaf Button 禁用更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377341/