thymeleaf 复选框回显_Thymeleaf+layui+jquery复选框回显

本文介绍了如何在Thymeleaf模板下,利用layui和jquery实现复选框的回显功能。通过将数组转化为JSON在后台与前台之间传递,然后在前台解析JSON并设置复选框的状态,最后使用layui的表单渲染确保显示正确。
摘要由CSDN通过智能技术生成

一、Thymeleaf+layui+jquery复选框回显

基于Thymeleaf模板下的layui+jquery复选框回显,主要是jquery。大致意思是:把数组转成JSON传到前台,再在前台转回数组 AJAX一般都是用JSON格式或XML格式来传递数据的JSON就是一种具有特殊格式的字符串。

1.实体类属性

1 //顾客等级

2 private Integer[] constomerGradeArray;3 //用来存储json格式的顾客等级数组(方便数据传输)

4 private String constomerGradeString;

View Code

2.后台返回

@RequestMapping("goodsTypeList")

public String goodsType_list(Client client,Model model){

if(client!=null){

//将数组转为json格式

client.setConstomerGradeString(JSON.toJSONString(client.getConstomerGradeArray()));

model.addAttribute("client",client);

}

return "goodsType_list";

}

3.前台页面

客户等级

4.jquery

layui.use([‘form‘,‘jquery‘], function(){

var form = layui.form;

var $ = layui.jquery;

$(function () {

if(‘[[${client.constomerGradeString}]]‘!=‘null‘){

//获取后台json /*用jQuery处理传过来的json值*/

var constomerGradeString=$.parseJSON(‘[[${client.constomerGradeString}]]‘);

//获取所有复选框的值

var constomerGradeArray = $("input[name=‘constomerGradeArray‘]");

//遍历json数组

$.each(constomerGradeString,function(i,json){

//获取所有复选框对象的value属性,用json数组和他们匹配,如果有,则说明他应被选中

$.each(constomerGradeArray,function(j,checkbox){

//获取复选框的value属性

var checkValue=$(checkbox).val();

if(json==checkValue){

$(checkbox).attr("checked",true);

}

})

//重新渲染(很重要:因为页面是用layui画的)

form.render();

})

}

})

})

}

参考链接:

json转换:https://www.cnblogs.com/YeHuan/p/11221504.html 或 https://blog.csdn.net/qq_37444478/article/details/76209189

主要代码:https://blog.csdn.net/w18853851252/article/details/82888109

表单渲染:https://blog.csdn.net/qq_33048333/article/details/80609553

原文:https://www.cnblogs.com/wwct/p/12144257.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值