How to capture enter key being pressed on pages containing multiple forms?

后端 未结 4 1079
既然无缘
既然无缘 2021-01-31 20:57

I have inherited a web application where the usual ability to press return in any of the input fields has been disabled for the very good reason that the page contains multiple

4条回答
  •  半阙折子戏
    2021-01-31 21:24

    Fingers crossed this'll work

    $(document).ready(function() {
      var focussed_form;
      $('input').focus(focus_form);
      $('input').blur(unfocus_form);
      $('textarea').focus(focus_form);
      $('textarea').blur(unfocus_form);
      $('select').focus(focus_form);
      $('select').blur(unfocus_form);
    
      $(document).keypress(function(e) {
        if(e.keyCode == 13) 
        {
          $(focussed_form).trigger('submit'); 
        }
      });
    
      function focus_form(){
        focussed_form = $(this).closest('form');
      }
    
      function unfocus_form(){
        focussed_form = NULL;
      }
    
    });
    

提交回复
热议问题