该文章由作者精心整理与总结,详细记录了对问题的思考与解决过程,原创文章,转载请在文章明显位置注明出处与原文链接!大家一起成长!!!
1、通过下了方式获取(无法取到值)
var list=[[${currMorList}]] //无法取到值
var list='[[${currMorList}]] ' //同样无法取到值
2、通过隐藏域获取
//thymleaf中div设置隐藏域
<div>
<input id="currMorList" th:type="hidden" th:value="${currMorList}">
</div>
//script中获取
<script>
layui.use(['layer', 'miniTab','echarts','carousel'], function () {
//1、通过$('#currMorList').val 获取
var curr=$('#currMorList').val //获取到的值是html文本
//2、另一种,(本文使用方法)
var curr=document.getElementById('currMorList').value //得到正确的文本json内容
})
</script>
3、解析获取到的list
通过以下方式解析时候报错:
$.each(curr,function(index,item){
var card11='<tbody class="card1-notice" ><tr BGCOLOR="#d5ffef"><td>'+item.Name+
' </td><td>'+item.Dept+
'</td><td> '+item.others+
'</td></tr></tbody>';
$('#card1').append(card11);
});
//--------------------------------------------------------
//以下报错内容:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"endDate":"2022-03-26 17:00:00","Dept":"办公室","Name":"王五","startDate":"2022-03-24 08:00:00","others":"Tue Jun 03 00:00:00 CST 2138到Tue Jun 03 00:00:00 CST 2138"},{"endDate":"2022-03-29 12:00:00","Dept":"科室","Name":"张三","startDate":"2022-03-25 08:00:00","others":"Tue Jun 03 00:00:00 CST 2138到Tue Jun 03 00:00:00 CST 2138"}]
at n (VM363 layui.js:2:41840)
at Function.each (VM363 layui.js:2:58719)
//使用以下方式重新格式化以下json字符串,问题解决-------------------------------------
var cmList=JSON.parse(document.getElementById('currMorList').value);
4、对应的controller
@GetMapping("/getList")
public String main(ModelMap model) throws ParseException {
//此处省略其他代码.................
model.addAttribute("currMorList",JSONUtil.parse(currMorList));
return "index";
}