在 Thymeleaf 中使用 JavaScript 变量
比方说,我有一个变量 b 和 c in:
<script th:inline="javascript">
let c = 20;
let b = 30;
document.getElementById("b").innerHTML = b;
document.getElementById("c").innerHTML = c;
</script>
当我尝试使用 : 将它们打印在我的页面上时,
<h1>"The value for b is: " <span id="b"></span></h1>
<h1>"The value for c is: " <span id="c"></span></h1>
这有效并且这些变量的值显示在页面上。 现在我正在使用 Thymeleaf 和 SPring,我有一个像这样的控制器方法:
@GetMapping("/test")
public String test( double val, double val2,
@RequestParam double rand, Model model) {
model.addAttribute("val", val);
model.addAttribute("val2", val2);
model.addAttribute("rand", rand);
model.addAttribute("distance", distance);
System.out.println(val);
System.out.println(val2);
return "/Test";
}
我只想输入 rand、val 和 val2 应该具有 JavaScript 变量(b 和 c)中的值:
<form th:action="@{/test}" method="get"><br>
<input type="text" id="b" th:name="val" " th:value="${b}">
<input type="text" id="c" th:name="val2" " th:value="${c}">
<label for="rand">rand:</label>
<input type="number" id="rand" th:name="rand" placeholder="rand" th:default="0"><br>
<input type="submit" value="Submit" onclick="add()">
<input type="reset" value="Reset">
</form>
但我没有得到正确的值来自JavaScript代码。所以我想将变量b和c赋值给变量val和val2,这样我只需要输入请求参数rand即可。 有没有人对此有解决方案。我查了一下,所有情况都是他们想在脚本中分配 Thymeleaf 变量,而不是相反。 提前致谢
Lets say for example I have a variable b and c in:
<script th:inline="javascript">
let c = 20;
let b = 30;
document.getElementById("b").innerHTML = b;
document.getElementById("c").innerHTML = c;
</script>
When I try to print them out on my page with :
<h1>"The value for b is: " <span id="b"></span></h1>
<h1>"The value for c is: " <span id="c"></span></h1>
This works and the values of these varaibles are shown on the page.
Now that I'm using Thymeleaf and SPring I have a Controller methode like this:
@GetMapping("/test")
public String test( double val, double val2,
@RequestParam double rand, Model model) {
model.addAttribute("val", val);
model.addAttribute("val2", val2);
model.addAttribute("rand", rand);
model.addAttribute("distance", distance);
System.out.println(val);
System.out.println(val2);
return "/Test";
}
I want to input only the rand, val and val2 should have the values from the JavaScript variables(b and c):
<form th:action="@{/test}" method="get"><br>
<input type="text" id="b" th:name="val" " th:value="${b}">
<input type="text" id="c" th:name="val2" " th:value="${c}">
<label for="rand">rand:</label>
<input type="number" id="rand" th:name="rand" placeholder="rand" th:default="0"><br>
<input type="submit" value="Submit" onclick="add()">
<input type="reset" value="Reset">
</form>
But I am not getting the correct values from the JavaScript code.So I want to assign the variables b and c to the variables val and val2 so that I only need to input the request parameter rand.
Has anyone a solution for this.I looked it up and all the cases were that they wanted to assign the Thymeleaf variable in the script and not the other way around.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论