jQuery get input value after keypress

后端 未结 9 1501
野趣味
野趣味 2020-11-28 21:40

I have the following function:

$(document).ready(function() {
    $(\"#dSuggest\").keypress(function() {
        var dInput = $(\'input:text[name=dSuggest]\'         


        
相关标签:
9条回答
  • 2020-11-28 21:53

    You have to interrupt the execution thread to allow the input to update.

      $(document).ready(function(event) {
           $("#dSuggest").keypress(function() {
               //Interrupt the execution thread to allow input to update
                   setTimeout(function() {
                       var dInput = $('input:text[name=dSuggest]').val();
                       console.log(dInput);
                       $(".dDimension:contains('" + dInput + "')").css("display","block");
                   }, 0);
           });
      });
    
    0 讨论(0)
  • 2020-11-28 21:55

    Use .keyup instead of keypress.

    Also use $(this).val() or just this.value to access the current input value.

    DEMO here

    Info about .keypress from jQuery docs,

    The keypress event is sent to an element when the browser registers keyboard input. This is similar to the keydown event, except in the case of key repeats. If the user presses and holds a key, a keydown event is triggered once, but separate keypress events are triggered for each inserted character. In addition, modifier keys (such as Shift) trigger keydown events but not keypress events.

    0 讨论(0)
  • 2020-11-28 21:56

    Realizing that this is a rather old post, I'll provide an answer anyway as I was struggling with the same problem.

    You should use the "input" event instead, and register with the .on method. This is fast - without the lag of keyup and solves the missing latest keypress problem you describe.

    $('#dSuggest').on("input", function() {
        var dInput = this.value;
        console.log(dInput);
        $(".dDimension:contains('" + dInput + "')").css("display","block");
    });
    

    Demo here

    0 讨论(0)
  • 2020-11-28 21:59

    I think what you need is the below prototype

    $(element).on('input',function(){code})
    
    0 讨论(0)
  • 2020-11-28 22:04

    I was looking for a ES6 example (so it could pass my linter) So for other people who are looking for the same:

    $('#dSuggest').keyup((e) => {
        console.log(e.currentTarget.value);
    });
    

    I would also use keyup because you get the current value that is filled in.

    0 讨论(0)
  • 2020-11-28 22:05

    jQuery get input value after keypress

    https://www.tutsmake.com/jquery-keypress-event-detect-enter-key-pressed/

    i = 0;  
    $(document).ready(function(){  
        $("input").keypress(function(){  
            $("span").text (i += 1);  
        });  
    }); 
    <!DOCTYPE html>  
    <html>  
    <head>  
    <title>jQuery keyup() Method By Tutsmake Example</title> 
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    </head>  
    <body>  
    Enter something: <input type="text">  
    <p>Keypresses val count: <span>0</span></p>  
    </body>  
    </html>  

    0 讨论(0)
提交回复
热议问题