jQuery数据结构渲染(4):复选框checkbox赋值

简介: jQuery数据结构渲染(4):复选框checkbox赋值

1:当返回值是字符串的时候

data.json:

{   
    "circle": "2;3;4;5;6;1" 
}

示例代码:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>复选框checkbox自定义样式</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
        <style>
            input[type=checkbox]{
                    appearance:none;
                    -moz-appearance:none; /* Firefox */
                    -webkit-appearance:none;                 
                    cursor: pointer;
                    margin:0;
                }                   
                input[type=checkbox]{
                    width:13px;
                    height:13px;                  
                    background: url(images/check.png);
                    background-size: 100% 100%;
                }           
                input[type=checkbox]:checked{
                    background: url(images/checked.png);
                    background-size: 100% 100%;
                }
    </style>
    </head>
    <body>
        <div class="modal-body form">
            <div class="col-md-7 col-sm-7  col-xs-7">
                <input name="circle" type="checkbox" value="2" />周一
                <input name="circle" type="checkbox" value="3" />周二
                <input name="circle" type="checkbox" value="4" />周三
                <input name="circle" type="checkbox" value="5" />周四
                <input name="circle" type="checkbox" value="6" />周五
                <input name="circle" type="checkbox" value="7" />周六
                <input name="circle" type="checkbox" value="1" />周日
            </div>
        </div>
    </body>
    <script type="text/javascript">
        $.ajax({
            url: "data.json",
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                $.each(data.circle.split(";"), function(i, item) {
                    $('input[name="circle"][value="' + item + '"]').attr('checked', 'checked')                              
                }) 
            }
        });
    </script>
</html>

2:当返回值是数组对象的时候

data.json:

{   
    "tasks": [{
        "id": 1,
        "name": "任务一"       
    }, {
        "id": 2,
        "name": "任务二"       
    },{
        "id": 3,
        "name": "任务三"       
    },{
        "id": 4,
        "name": "任务四"       
    }]
}

参考代码:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>复选框赋值</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
        <style>
            input[type=checkbox]{
                    appearance:none;
                    -moz-appearance:none; /* Firefox */
                    -webkit-appearance:none;                 
                    cursor: pointer;
                    margin:0;
                }                   
                input[type=checkbox]{
                    width:13px;
                    height:13px;                  
                    background: url(images/check.png);
                    background-size: 100% 100%;
                    margin-right:8px;
                }           
                input[type=checkbox]:checked{
                    background: url(images/checked.png);
                    background-size: 100% 100%;
                }
    </style>
    </head>
    <body>
        <div class="workQueryList">
        </div>
    </body>
    <script type="text/javascript">
        $.ajax({
            url: "data.json",
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                //checkbox渲染
                var htmlQuery = "";
                $.each(data.tasks,function(i, item) {
                    htmlQuery += '<input name="query" type="checkbox" checked="checked" value="' + item.id + '" /><span>'
                    + item.name
                    + '</span></br>';
                })
                $(".workQueryList").html(htmlQuery)
            }
        });
    </script>
</html>

显示如下


3:当返回值是数组字符串的时候

{
    "permTokens": ["site", "alarm", "distribution", "my"],  
}

示例代码

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>复选框赋值</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
        <style>
            input[type=checkbox]{
                    appearance:none;
                    -moz-appearance:none; /* Firefox */
                    -webkit-appearance:none;                 
                    cursor: pointer;
                    margin:0;
                }                   
                input[type=checkbox]{
                    width:13px;
                    height:13px;                  
                    background: url(images/check.png);
                    background-size: 100% 100%;
                    margin-right:8px;
                }           
                input[type=checkbox]:checked{
                    background: url(images/checked.png);
                    background-size: 100% 100%;
                }
    </style>
    </head>
    <body>
        <div class="col-md-5 col-sm-5  col-xs-5  workModelList">
            <input type="checkbox" name='appModel' value="site">位置
            <input type="checkbox" name='appModel' value="alarm">告警
            <input type="checkbox" name='appModel' value="distribution">分布
            <input type="checkbox" name='appModel' value="my">我的
            <input type="checkbox" name='appModel' value="work">作业管理
            <input type="checkbox" name='appModel' value="partol">电子巡更
            <input type="checkbox" name='appModel' value="person">员工管理
        </div>
    </body>
    <script type="text/javascript">
        $.ajax({
            url: "data.json",
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                $.each(data.permTokens, function(i, item) {
                    $('input[value="' + item + '"]').attr("checked", "checked")
                })
            }
        });
    </script>
</html>

相关实践学习
Serverless极速搭建Hexo博客
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
5月前
|
存储
数据结构---串(赋值,求子串,比较,定位)
数据结构---串(赋值,求子串,比较,定位)
40 4
数据结构---串(赋值,求子串,比较,定位)
|
4月前
|
JavaScript
jQuery如何验证复选框是否选中
jQuery如何验证复选框是否选中
22 0
|
5月前
|
JavaScript 前端开发
jquery怎么给循环出来的列表(类似于text框)取值和赋值
jquery怎么给循环出来的列表(类似于text框)取值和赋值
35 0
jquery怎么给循环出来的列表(类似于text框)取值和赋值
|
5月前
|
存储 Java
数据结构:图文详解顺序表的各种操作(新增元素,查找元素,删除元素,给指定位置元素赋值)
数据结构:图文详解顺序表的各种操作(新增元素,查找元素,删除元素,给指定位置元素赋值)
186 0
|
11月前
|
JSON JavaScript 数据格式
jQuery将json性别数据int类型进行格式化渲染
jQuery将json性别数据int类型进行格式化渲染
40 0
|
11月前
|
JavaScript
jQuery数据渲染,每行显示4个数据,超过四条数据自动换行
jQuery数据渲染,每行显示4个数据,超过四条数据自动换行
38 0
|
6天前
|
算法 安全 测试技术
golang 栈数据结构的实现和应用
本文详细介绍了“栈”这一数据结构的特点,并用Golang实现栈。栈是一种FILO(First In Last Out,即先进后出或后进先出)的数据结构。文章展示了如何用slice和链表来实现栈,并通过golang benchmark测试了二者的性能差异。此外,还提供了几个使用栈结构解决的实际算法问题示例,如有效的括号匹配等。
golang 栈数据结构的实现和应用
01_设计一个有getMin功能的栈
01_设计一个有getMin功能的栈
|
6天前
|
前端开发
07_用队列实现栈
07_用队列实现栈
06_用栈来求解汉诺塔问题
06_用栈来求解汉诺塔问题