当前位置:首页 > Web开发 > 正文

标签: 一、Thymeleaf+layui+jquery复选框回显 基于Thymeleaf模板下的layui+jque

2024-03-31 Web开发

标签:

一、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.前台页面<div>

<label>客户品级</label> <div> <!-- 如果是layui的表单提交input标签的name值是constomerGradeArray[] --> <!-- 不然就会导致只提交过去一个值 (本人使用var data=$("form").serialize();) -->
     <input type="checkbox" value="1" title="高级客户">
     <input type="checkbox" value="2" title="VIP客户">
  </div>
</div>

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

  

Thymeleaf+layui+jquery复选框回显

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32350.html