龙空技术网

前端js获取SpringMvc后台model中传值

早起的年轻人 127

前言:

今天兄弟们对“js如何获取标签的值”都比较讲究,各位老铁们都需要知道一些“js如何获取标签的值”的相关文章。那么小编在网络上汇集了一些有关“js如何获取标签的值””的相关文章,希望朋友们能喜欢,兄弟们快快来了解一下吧!

使用 SpringBoot +SpringMVC +thymeleaf 组合实现的功能,期望在 thymeleaf 中的html中的js中 获取 springboot 中 Model 中设置的值

@Controller@RequestMapping("/web/computerTest")public class ComputerTestWebController {    /**     * 选择考试     */    @GetMapping("/chooseExam.html")    public String chooseExam(@RequestParam String token, Model model) {              Boolean showV1 = true;        Boolean showPre = true;             model.addAttribute("showV1", showV1);        model.addAttribute("showPre", showPre);        //对应显示的html        return "computertestweb/chooseExamV2";    }}

这是我对应的html文件

有两种方式:

1 内联js方式

<script th:inline="javascript">    let activityName = "first"        if([[${showV1}]]){        activityName = "first"    }else{        activityName = "second"    }</script>
2、使用隐藏域,

使用隐藏域, 先把model的值通过标签的方式放到某个input标签下,再到js中通过js或者jquery按照id的方式选取

2.1 在html 中写入隐藏标签

<input type="hidden" id="showV1" value="${showV1}}">
2.2 在js中通过ID获取标签获取值
let data= $("#showV1").val();

标签: #js如何获取标签的值 #js获取model #springmvc读取图片到前端显示