简体   繁体   中英

Disable input when enter key pressed multiple times

How do I make so that when the enter key is pressed multiple times, the site only detects the input once? I have a small bug which occurs whenever the user double taps the enter key in an input box, so what I want to do is only accept the input press once. Thank you so much in advance!!

  $('#search_list').keypress(function(e) {
    if (e.which == '13') {


   }    
}

You can prevent repeatedly enter pressing by this way:

var enterPressed = false;

$('#search_list').keypress(function(e) {
    if (e.which == '13') {
       if( ! enterPressed){
         // do some processing
         return;
       }
      enterPressed = true;
      setTimeout(function(){
         enterPressed = false;
      }, 1000);

   }    
}

Inspired by this post .

var enterPressed = false;

$('#search_list').keypress(function(e) {

    if (e.which == '13') {
       if( ! enterPressed){
         // do some processing

          enterPressed = true;
          setTimeout(function(){
            enterPressed = false;
          }, 1000);

        }


   }    
}

I think this way is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM