Search jquery live

How can I check the value of an input field continuously after it has been focused once? (with jquery). I am looking to do a direct search and completely remove the submit button in my form.

+4
source share
5 answers

I would bind keyup and mouseup events to input

$("#search").keyup(Search).mouseup(Search); function Search(event) { // keyup for keyboard entry // mouseup for copy-pasting with the mouse } 

After dealing with this in jsfiddle, I finally came up with the following:

 $("#search").keyup(function(e) { Search(e); }).bind("paste", function(e) { // have to do this so the DOM can catch up on mouse right-click paste setTimeout(function() { Search(e); }, 100); }); 

I didn't know about the paste event, but obviously it's awesome

Working example: http://jsfiddle.net/rLAxL/1/

+8
source

Bind a keyup event to check the contents and make a request when the content changes. To throttle requests, use setTimeout .

 $("#input").keyup(function(e){ if (window.liveSearch) clearTimeout(window.liveSearch); window.liveSearch = setTimeout(function(){ // request results here }, 1000); // delay time in ms }); 

To improve performance, you can save the search by object (for example, the cache ["some search"] = results), and if the same request is made, you do not need to query the server again.

+2
source
 $('#<element>').bind('keyup', function() { //do something }); 
+1
source

setInterval comes to mind.

Or you can just look at events like onKeyUp or onKeyDown or onClick, or onChange.

0
source

Try the following:

 $("#search").on("keyup",function(){ Search(...); } 

The thing is, you need to make an avatar function that calls your function that executes the code

or put this html as your input html:

 <input type="text" onKeyUp="Search(this.value)"/> 
0
source

Source: https://habr.com/ru/post/1334565/


All Articles