开发者

jquery live search

How i can i check the value of an input field continuously after it has been focused once ? (with jquery).I'm looking to make a live search and completely remove the submit button o开发者_Go百科n my form.


I would bind the keyup and mouseup events to the input

$("#search").keyup(Search).mouseup(Search);

function Search(event)
{
    // keyup for keyboard entry
    // mouseup for copy-pasting with the mouse        
}

After fighting with this in jsfiddle I finally came up with this:

$("#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 wasn't aware of the paste event, but clearly, it is awesome

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


Bind a keyup event to check the content and make a request when the content is changed. To throttle the requests use a 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 on an object (e.g. cache["some search"] = results) and if the same request is made, you don't have to request the server again.


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


setInterval comes to mind.

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


Try this:

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

the thing is that you need to make an avatar function that call your function that do the code

or put this html as your input html:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜