Layui’s method of implementing input input and selection:
HTML code:
<div class="layui-col-md4"> <label class="layui-form-label">移交单位<span style="color:red">*</span></label> <div class="layui-input-block"> <input type="text" name="HandoverCompany" id="HandoverCompany" class="layui-input" style="position:absolute;z-index:2;width:80%;" lay-verify="required" value="111" onkeyup="search()" autocomplete="off"> <select type="text" id="hc_select" lay-filter="hc_select" autocomplete="off" placeholder="移交单位全称" lay-verify="required" class="layui-select" lay-search> <option value="111">111</option> <option value="222">222</option> <option value="333">333</option> <option value="444">444</option> <option value="555">555</option> </select> </div> </div>
Let’s briefly talk about several styles of input .
position:absolute here is to put input and select at the same position.
z-index:2 is to put the input on the select.
width:80% is to not cover the small triangle symbol behind the select, and the select can still be clicked.
autocomplete="off" In order not to automatically fill in the input box, so as not to cover the select option
and then the JS code.
layui.use(['form', 'layedit','upload'], function () { var form = layui.form form.on('select(hc_select)', function (data) { //选择移交单位 赋值给input框 $("#HandoverCompany").val(data.value); $("#hc_select").next().find("dl").css({ "display": "none" }); form.render(); }); window.search = function () { var value = $("#HandoverCompany").val(); $("#hc_select").val(value); form.render(); $("#hc_select").next().find("dl").css({ "display": "block" }); var dl = $("#hc_select").next().find("dl").children(); var j = -1; for (var i = 0; i < dl.length; i++) { if (dl[i].innerHTML.indexOf(value) <= -1) { dl[i].style.display = "none"; j++; } if (j == dl.length-1) { $("#hc_select").next().find("dl").css({ "display": "none" }); } } } });
Let me briefly explain my idea. First, the value selected by select must be assigned to the input box. If so, you need form.on('select(hc_select)' to monitor the change of the select value. After selecting, you need to Hide the drop-down list. At the same time, re-render the form. It is also possible to re-render only the current select.
Then the text entered into the input box is searched in the select. First, we check It can be found in the dom structure of select that the options in it are all in the dd tag under dl, as shown in the figure.
Then we get the dl tag, and then loop through The way to match one by one is whether there is a relationship between the options in dd and the text we input. Just use indexOf. If they are not similar, they will be hidden directly. Then why do we need to define a j here?
Because if there is no relationship If there is a match, an empty dl tag will still appear below. The page display is an empty small list, which affects the appearance a bit, so if the text you enter and the options in the drop-down list do not matter, just hide the dl directly. Here I judge that if the number of dissimilar items is equal to dl.length, it means that none of the text you input is similar to the selected option, and then dl can be hidden.
For more layui knowledge, please Pay attention to the layui usage tutorial column on the PHP Chinese website.
The above is the detailed content of Layui implements input input and selection methods. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Layui's flow module for infinite scrolling, covering setup, best practices, performance optimization, and customization for enhanced user experience.

The article details how to use Layui's element module to create and customize UI elements like tabs, accordions, and progress bars, highlighting HTML structures, initialization, and common pitfalls to avoid.Character count: 159

The article discusses customizing Layui's carousel module, focusing on CSS and JavaScript modifications for appearance and behavior, including transition effects, autoplay settings, and adding custom navigation controls.

The article guides on using Layui's carousel module for image sliders, detailing steps for setup, customization options, implementing autoplay and navigation, and performance optimization strategies.

The article discusses configuring Layui's upload module to restrict file types and sizes using accept, exts, and size properties, and customizing error messages for violations.

The article explains how to use Layui's layer module to create modal windows and dialog boxes, detailing setup, types, customization, and common pitfalls to avoid.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool