search
HomeWeb Front-endJS TutorialLet the input box implement a search prompt similar to Baidu (based on jquery event monitoring)_jquery

A very cool effect. Baidu and Google seem to have implemented it for many years. I thought I could easily find the code to achieve this effect on the Internet. When I really encountered this need, I found that I really couldn’t find it. So I wrote this effect myself. Since I integrated the effect into my entire framework, I did not encapsulate it separately.

Requirements:
Implement an input box with prompts, similar to Baidu search, to obtain commonly used keywords when there are changes. The data comes from the system database and supports mouse selection or keyboard selection

Idea:
The consistent idea of ​​the framework is to use class as the monitoring entrance and data as the data transmission;
Real-time change monitoring is achieved by monitoring input and propertychange events. Input is the mainstream, propertychange is ie, you know ;
Implement the post action through ajax, and display the returned content in a form similar to a selection box;
Monitor the up key (38), down key (40), and enter key (13) of the keyboard, and bind keydown , determine the event.keycode implementation;
Monitor the mouseover and click events of the mouse, which must be perfectly integrated with the keyboard actions;
If the input content requirements must be consistent with the known options, monitor the blur event to determine whether focus change is allowed;

Implementation code:

Copy code The code is as follows:

//By COoL

//Define global variables to store the prompt layer
var liketips;

//Listen for changes or get focus events

//Disable the automatic prompts that come with Chrome and Firefox browsers
$('.getsearchjson').attr("autocomplete", "off");
$('.getsearchjson').bind( "propertychange input focus",function(event){
$this=$(this);
if(event.type!='focus'){
//If there is a change, the status is set to It must be selected again, because pure manual input will cause the value to be unable to be inserted
$this.data('ok',false);
}

//Get the input box position and calculate the prompt layer should appear Position
var top=1*$this.offset().top 25;
var left=1*$this.offset().left;
var width=1*$this.width( ) 12;

//Rebuild the storage tip layer and display it in the appropriate position
$(liketips).remove();
liketips=document.createElement('div');
$liketips=$(liketips);
//class style is not provided here, the most important thing is position:absolute
$liketips.addClass("liketips");
$liketips.css({top: top 'px',left:left 'px',width:width 'px'});

//Display the loading dynamic image before loading
$liketips.append('Let the input box implement a search prompt similar to Baidu (based on jquery event monitoring)_jquery');
$liketips.appendTo($this.parent());
$liketips.show();

//Define ajax to obtain json, the type parameter is set through data-type, and the keyword is the value that has been entered so far
//The return value is displayed in table form
$.post('/data/search.do ',{type:$this.data('type'),keyword:$this.val()},function(json){
$liketips.empty();
var htmlcode="";
for(var i=0;i //I need to use value here and title, so use data-value to pass one more parameter, and assign the value to the corresponding place after pressing Enter or mouse click, which perfectly replaces select
htmlcode ='
'; ;/tbody>
' json.datas[i][1] '
Please be sure to select" in this drop-down box;
//Replace the loading dynamic image with content
$liketips.html(htmlcode) ;
},"json");
});

//When the focus disappears, make sure the data comes from the option and hide the prompt box
$('.getsearchjson').blur( function(){
//Because the blur action is settled before the click when the mouse is clicked, setTimeout is to solve this problem
$oldthis=$(this);
setTimeout(function(){
if ($oldthis.data('ok'))
                $(liketips)                                                                                                                                                                 One item, thank you for your cooperation');
                       $oldthis.focus();                      );

//Monitor keyboard actions
$('.getsearchjson').keydown(function(event){
//console.log(event.keyCode);
$this =$(this);
if(event.keyCode==40){
//The key is down
$nowtr=$('tr.selectedtr');
//If it has been If there is selection, go down, otherwise, select the first
in the menu if($nowtr.length>0){
$nexttr=$nowtr.next('tr')
The last option, move to the next tr, otherwise jump to the first tr
                                                                             .addClass('selectedtr');
                                               ; tr:first' ).addClass('selectedtr');
                                    ; 🎜>                                                                                                                 If already selected, go down, otherwise, select the first
in the menu if($nowtr.length>0){
$prevtr=$nowtr.prev('tr')
//If It is not the last option, move to the next tr, otherwise jump to the first tr
                                                                                                                prevtr.addClass ('selectedtr'); ('tr:last ').addClass('selectedtr');
                                                        ;
                                                                                                                                               selectedtr');
if($nowtr.length==1){
//Set the value to the value storage item configured by the input box through the parameter data-valueto, usually the hidden item
$valuefield=$('input[name=' $this.data('valueto') ']');
           $valuefield.val($nowtr.data('value')); val($nowtr.text());
                                                                                                                                     ).fadeOut('fast');
       return false;
    }
//console.log(event.keyCode);
return true;
});

//Listen to mouse movements, mouseover changes the selected item
$(document). delegate('.liketips td','mouseover',function(){
$nowtr=$(this).parent();
$nowtr.siblings('tr').removeClass();
$nowtr.addClass('selectedtr');
});

//Listen to mouse movements, click to select
$(document).delegate('.liketips td','click ',function(){
$nowtr=$(this).parent();
if($nowtr.length==1){
//Get the input box corresponding to the prompt layer
$ InputField = $ nowtr.parent (). Parent (). Parent (). Siblings ('Input.getsearchjson'); lue The value is stored in the item, usually the hidden item
$valuefield=$('input[name=' $inputfield.data('valueto') ']');
$valuefield.val($nowtr.data ('value'));
$inputfield.val($nowtr.text());
$inputfield.data('ok', true);
}
$(liketips).fadeOut('fast');
});



The CSS will not be released here. My implementation is as follows:



Let the input box implement a search prompt similar to Baidu (based on jquery event monitoring)_jquery

Copy code The code is as follows: //Disable the automatic prompts that come with chrome and firefox browsers
$this.attr("autocomplete","off");


Statement
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
laravel input隐藏域怎么实现laravel input隐藏域怎么实现Dec 12, 2022 am 10:07 AM

laravel input隐藏域的实现方法:1、找到并打开Blade模板文件;2、在Blade模板中使用method_field方法来创建隐藏域,其创建语法是“{{ method_field('DELETE') }}”。

vue3怎么封装input组件和统一表单数据vue3怎么封装input组件和统一表单数据May 12, 2023 pm 03:58 PM

准备工作用vuecreateexample创建项目,参数大概如下:用原生input原生的input,主要是value和change,数据在change的时候需要同步。App.tsx如下:import{ref}from'vue';exportdefault{setup(){//username就是数据constusername=ref('张三');//输入框变化的时候,同步数据constonInput=;return()=>({

PHP开发:如何实现搜索关键词提示和自动补全功能PHP开发:如何实现搜索关键词提示和自动补全功能Sep 21, 2023 pm 01:01 PM

PHP开发:实现搜索关键词提示和自动补全功能在现今互联网时代,搜索引擎已经成为人们获取信息的重要渠道之一。而在网站开发中,搜索功能的重要性不言而喻。为了提升用户体验和搜索效果,实现搜索关键词提示和自动补全功能是一项非常有价值和必要的开发任务。本文将为大家介绍如何在PHP开发中实现搜索关键词提示和自动补全功能,并提供具体的代码示例。搜索关键词提示是指在用户输入

在JavaScript中,"oninput"事件的用途是什么?在JavaScript中,"oninput"事件的用途是什么?Aug 26, 2023 pm 03:17 PM

当在输入框中添加值时,就会发生oninput事件。您可以尝试运行以下代码来了解如何在JavaScript中实现oninput事件-示例<!DOCTYPEhtml><html>&nbsp;&nbsp;<body>&nbsp;&nbsp;&nbsp;<p>Writebelow:</p>&nbsp;&nbsp;&nbsp;<inputtype="text"

Vue文档中的input框绑定事件详解Vue文档中的input框绑定事件详解Jun 21, 2023 am 08:12 AM

Vue.js是一种轻量级的JavaScript框架,具有易用、高效和灵活的特点,是目前广受欢迎的前端框架之一。在Vue.js中,input框绑定事件是一个十分常见的需求,本文将详细介绍Vue文档中的input框绑定事件。一、基础概念在Vue.js中,input框绑定事件指的是将输入框的值绑定到Vue实例的数据对象中,从而实现输入和响应的双向绑定。在Vue.j

点击input框没有光标怎么办点击input框没有光标怎么办Nov 24, 2023 am 09:44 AM

点击input框没有光标的解决办法:1、确认输入框焦点;2、清除浏览器缓存;3、更新浏览器;4、使用JavaScript;​5、检查硬件设备;6、检查输入框属性;7、调试JavaScript代码;8、检查页面其他元素;9、考虑浏览器兼容性。

Vue文档中的input框回车事件和验证函数使用方法Vue文档中的input框回车事件和验证函数使用方法Jun 20, 2023 am 09:13 AM

Vue是一个流行的JavaScript前端框架,它的核心是响应式数据绑定和组件系统。在Vue的应用程序中,input框是最常用的UI元素之一。在用户输入文本时,我们希望可以监听回车事件,并且在提交前对输入内容进行验证。本篇文章将介绍Vue文档中的input框回车事件和验证函数使用方法。一、Vue中input框回车事件在Vue中监听input框的回车事件非常简

jquery中input是什么元素jquery中input是什么元素Jun 06, 2023 pm 02:18 PM

jquery中input是选择器选取表单元素,其作用是:1、input用于搜集用户信息,根据不同的type属性值,输入字段拥有很多种形式,输入字段可以是文本字段、复选框、掩码后的文本控件、单选按钮、按钮等等;2、input标签可定义输入域的开始,在其中用户可输入数据;3、使用input标签来进行定义,其中包括文本字段、多选列表、可单击的图像和提交按钮等。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!