


Numeric input box with plus and minus buttons under layUI framework
Layui front -end framework is a more popular framework now. Many of the built -in modules are convenient and fast to use, but a few modules with fewer application scenarios are not built in it. The digital input box with the addition and subtraction button is suitable for shopping or Other scenarios that require the use of numbers.
The UI style of this extension module is completely based on layUI. It can be used alone or extended to the extension module of the layUI framework.
: After opening Layui's document, the beginning part wrote a supporting expansion custom module, and other modules built -in can also be used. In this way, there is no need to write this extension from scratch. Add two buttons to the original input box code, then bind click events to the two buttons, and then determine whether the value after each click meets the conditions, so that the addition and subtraction input boxes are complete.
The original method of use is to use the element ID and then instantiate the parameters. However, considering that a page may have multiple input boxes, it would be troublesome to write the instantiation code for each one, so the binding The process is built into the method. After instantiation, all input boxes on a page that require the use of plus and minus buttons will be rendered.
1.CSS part:
Put these lines of css code into the public style file.plus-minus .layui-input-block{position: relative;} .plus-minus input{position: absolute;top: 0px;left: 0px;text-align: center;} .plus-minus button:nth-of-type(1){position: absolute;top: 0px;left: 0px;height: 100%;} .plus-minus button:last-child{position: absolute;top: 0px;right: 0px;height: 100%;}
but ——The value that increases or decreases after clicking, the default is 1
## into a js file.
<div class="plus-minus"> <div class="layui-form-item"> <label class="layui-form-label">数量</label> <div class="layui-input-block"> <input type="number" name="num" data-step="1" data-maxvalue="20" data-minvalue="1" lay-verify="required" autocomplete="off" class="layui-input num"> </div> </div> </div>
4.js instantiated use
#
layui.define(['layer'], function(exports){ var $ = layui.$ var obj = { //数字加减函数(基本参数对象,最大值返回函数,最小值返回函数) plusminus : function (){ $(".plus-minus").each(function(){ //定义按钮HTML var plusminusbutton = '<button type="button" class="layui-btn layui-btn-sm layui-btn-normal vk-minus"><i class="fa fa-minus"></i></button>' +'<button type="button" class="layui-btn layui-btn-sm layui-btn-normal vk-plus"><i class="fa fa-plus"></i></button>'; var data = new Object; data.step = $(this).find('input').data('step'); data.maxvalue = $(this).find('input').data('maxvalue'); data.minvalue = $(this).find('input').data('minvalue'); //定义默认参数,合并参数 options = $.extend({ step: 1, //每次点击加减的值 maxvalue: false, //最大值,默认false,不限制 minvalue: false, //最小值,默认false,不限制 },data); var elem = $(this).find('input'), step = parseInt(options.step), maxvalue = options.maxvalue, minvalue = options.minvalue; //参数不规范则返回 if(elem == null || elem == undefined){return}; if(step == 0 || step == undefined){return}; //加入按钮HTML $(elem).after(plusminusbutton); //点击增加 $(elem).parent().on("click", ".vk-plus", function(){ var nowinput = $(this).siblings("input"), //当前输入框元素 nowbutton = $(this).siblings("button"), //当前按钮元素 oldval = $(nowinput).val(), //点击前的值 newval = parseInt(oldval) + step; //点击后的值 if(newval < maxvalue && newval > minvalue) { $(nowbutton).removeClass("layui-btn-disabled"); } //判断条件。是否最大值 if(maxvalue == false) { $(nowinput).val(parseInt(oldval)+step); } if(maxvalue != 0 && newval < maxvalue) { $(nowinput).val(parseInt(oldval)+step); } if(maxvalue != 0 && newval >= maxvalue) { $(nowinput).val(maxvalue); $(this).addClass("layui-btn-disabled"); } //模拟change事件 $(nowinput).trigger('change'); return; }); //点击减少(同上) $(elem).parent().on("click", ".vk-minus", function(){ var nowinput = $(this).siblings("input"), nowbutton = $(this).siblings("button"), //当前按钮元素 oldval = $(elem).val(), newval = parseInt(oldval) - step; if(newval < maxvalue && newval > minvalue) { $(nowbutton).removeClass("layui-btn-disabled"); } if(minvalue == false) { $(nowinput).val(parseInt(oldval)-step); } if(minvalue != 0 && newval > minvalue) { $(nowinput).val(parseInt(oldval)-step); } if(minvalue != 0 && newval <= minvalue) { $(nowinput).val(minvalue); $(this).addClass("layui-btn-disabled"); } //模拟change事件 $(nowinput).trigger('change'); return; }); }); } }; exports('common',obj); });
Summary: layUI has developed very well over the years. Once you are familiar with each module, you can quickly develop front-end pages without writing a lot of js code. For non-front-end professional developers, you can design relatively Friendly page.
I hope this article can help more friends.
The above is the detailed content of Numeric input box with plus and minus buttons under layUI framework. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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.

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.

To run layui, perform the following steps: 1. Import layui script; 2. Initialize layui; 3. Use layui components; 4. Import layui styles (optional); 5. Ensure script compatibility and pay attention to other considerations. With these steps, you can build web applications using the power of layui.

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 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.
