Home Web Front-end Layui Tutorial Introduction to using layui checkbox

Introduction to using layui checkbox

Nov 27, 2019 pm 01:09 PM
layui

Introduction to using layui checkbox

layui check box:

Rendering

Introduction to using layui checkbox

layui check box, a main check box The box controls multiple slave check boxes. The colors of the master check box and slave check boxes are different.

Introduction to using layui checkbox

The style of the layui check box is selected only after There will be, so it cannot be achieved directly through css settings. It can only be set dynamically through js

html code uses jfinal template

1

2

3

4

5

6

7

8

9

<div class="layui-inline">

              <label class="layui-form-label"><font class="faiqi-font-red-star">*</font>#(i18n.get(&#39;所属校区&#39;))</label>

              <div class="layui-input-block">

                  <input id="qx" lay-filter="allCheck" type="checkbox" value="" name="" title="#(i18n.get(&#39;全选&#39;))" >

                  #for(campus : campusList)

                      <input type="checkbox" lay-filter="campus" class="campus" value="#(campus.id)" name="campusIds[#(campus.id)]" title="#(campus.campusName)" #(campusIdStr.contains(&#39;,&#39; + campus.id + &#39;,&#39;) ? &#39;checked="checked"&#39;:&#39;&#39;)>

                  #end

              </div>

            </div>

Copy after login

layui code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

$(function(){

  

layui.use(&#39;form&#39;, function(){

    var form = layui.form;

    form.on("checkbox(allCheck)", function(data){

        console.log(data);

        console.log(data.elem.checked);

        if (data.elem.checked) {

            //动态设置全选按钮颜色,不可以这里设置,这里设置后,前端选然后不会有效果的,

            //猜测原因是,form.render("checkbox"); 导致的,设置后layui又渲染了,把我自己设置的颜色覆盖了。所以设置需要在渲染后再设置,就等于是用我的css覆盖了layui的css

            $(".campus").each(function(){

                $(this).prop(&#39;checked&#39;, true);

            });

        } else {

            $(".campus").each(function(){

                $(this).prop(&#39;checked&#39;,  false);

            });

        }

        form.render("checkbox");

       //渲染后设置我的颜色

        allCheckbox();

    });

  

    //查看是否被全选了,全选了,全选按钮编辑的时候就是被选中中状态

    function initselect(){

        let allSelect = true;

        $(".campus").each(function(index, elem){

            //每个checkbox添加点击事件,如果点击了,使得所有的按钮中出现了不被选中的,那么全选按钮就不被选中

            if($(this).prop(&#39;checked&#39;) == false){

                allSelect = false;<br>              

            }

        });

        console.log("是否全选",allSelect)

        $("#qx").prop(&#39;checked&#39;,allSelect);

        form.render("checkbox");

        //记得把设置事件放到渲染事件后

        allCheckbox();

    }

    initselect();

  

    //校区点击事件,如果有校区没有被选中,那么全选按钮就不能够显示选中状态

    form.on("checkbox(campus)", function(data){

        let checked = data.elem.checked;

        initselect();

    });

  

});

  

    //全选按钮和其他按钮的颜色不一样

    function allCheckbox(){

        qx1=$(&#39;#qx&#39;).next(&#39;div&#39;).children(&#39;span&#39;);

        if($(&#39;#qx&#39;).prop(&#39;checked&#39;)){

            //被选中就设置颜色

            qx1.css({

                &#39;background-color&#39;:&#39;#e4393c&#39;

            })

        }

    }

    //初始化设置全选按钮的颜色,

    allCheckbox();<br><br>})

Copy after login

css

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<style>

.layui-form-checkbox span {

        width:154px

    }

.layui-unselect.layui-form-checkbox{

        margin-bottom:5px;

    }

    .layui-form-checkbox span{

        color:#4C5277;

    }

    .layui-form-checked span{

        color:#fff;

    }

    /*.layui-form-checked span{

        background-color:#b31717!important;

    }*/

</style>

Copy after login

Please pay attention for more layui knowledgelayui usage tutorial column.

The above is the detailed content of Introduction to using layui checkbox. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up jump on layui login page How to set up jump on layui login page Apr 04, 2024 am 03:12 AM

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

How to get form data in layui How to get form data in layui Apr 04, 2024 am 03:39 AM

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

How layui implements self-adaptation How layui implements self-adaptation Apr 26, 2024 am 03:00 AM

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

How to transfer data in layui How to transfer data in layui Apr 26, 2024 am 03:39 AM

The method of using layui to transmit data is as follows: Use Ajax: Create the request object, set the request parameters (URL, method, data), and process the response. Use built-in methods: Simplify data transfer using built-in methods such as $.post, $.get, $.postJSON, or $.getJSON.

What is the difference between layui and vue? What is the difference between layui and vue? Apr 04, 2024 am 03:54 AM

The difference between layui and Vue is mainly reflected in functions and concerns. Layui focuses on rapid development of UI elements and provides prefabricated components to simplify page construction; Vue is a full-stack framework that focuses on data binding, component development and state management, and is more suitable for building complex applications. Layui is easy to learn and suitable for quickly building pages; Vue has a steep learning curve but helps build scalable and easy-to-maintain applications. Depending on the project needs and developer skill level, the appropriate framework can be selected.

What does layui mean? What does layui mean? Apr 04, 2024 am 04:33 AM

layui is a front-end UI framework that provides a wealth of UI components, tools and functions to help developers quickly build modern, responsive and interactive web applications. Its features include: flexible and lightweight, modular design, rich components, Powerful tools and easy customization. It is widely used in the development of various web applications, including management systems, e-commerce platforms, content management systems, social networks and mobile applications.

The difference between layui framework and vue framework The difference between layui framework and vue framework Apr 26, 2024 am 01:27 AM

layui and vue are front-end frameworks. layui is a lightweight library that provides UI components and tools; vue is a comprehensive framework that provides UI components, state management, data binding, routing and other functions. layui is based on a modular architecture, and vue is based on a componentized architecture. layui has a smaller ecosystem, vue has a large and active ecosystem. The learning curve of layui is low, and the learning curve of vue is steep. Layui is suitable for small projects and rapid development of UI components, while vue is suitable for large projects and scenarios that require rich functions.

What language is layui framework? What language is layui framework? Apr 04, 2024 am 04:39 AM

The layui framework is a JavaScript-based front-end framework that provides a set of easy-to-use UI components and tools to help developers quickly build responsive web applications. Its features include: modular, lightweight, responsive, and has complete documentation and community support. layui is widely used in the development of management backend systems, e-commerce websites, and mobile applications. The advantages are quick start-up, improved efficiency, and easy maintenance. The disadvantages are poor customization and slow technology updates.

See all articles