html catch event when user is typing into a text input

前端 未结 4 1446
梦毁少年i
梦毁少年i 2020-12-31 01:53

Im just wondering how I go about catching the event when the user is typing into a text input field on my web application.

Scenario is, I have a contacts listing gri

相关标签:
4条回答
  • 2020-12-31 02:24

    I would use the 'input' and 'propertychange' events. They fire on cut and paste via the mouse as well.

    Also, consider debouncing your event handler so that fast typists are not penalized by many DOM refreshes.

    0 讨论(0)
  • 2020-12-31 02:42

    see my try:

    you should put .combo after every .input classes.

    .input is a textbox and .combo is a div

    $(".input").keyup(function(){
        var val = this.value;
        if (val.length > 1) {
            //you search method...
        }
        if (data) $(this).next(".combo").html(data).fadeIn(); else $(this).next(".combo").hide().html("");
    });
    
    $(".input").blur(function(){
        $(this).next(".combo").hide();
    });
    
    0 讨论(0)
  • 2020-12-31 02:43

    Use the keyup event to capture the value as the user types, and do whatever it is you do to search for that value :

    $('input').on('keyup', function() {
         if (this.value.length > 1) {
              // do search for this.value here
         }
    });
    

    Another option would be the input event, that catches any input, from keys, pasting etc.

    0 讨论(0)
  • 2020-12-31 02:44

    Why not use the HTML oninput event?

    <input type="text" oninput="searchContacts()">
    
    0 讨论(0)
提交回复
热议问题