Clicking a link when enter key is pressed using jQuery

后端 未结 7 1940
无人及你
无人及你 2021-02-19 22:58

I\'m trying to make it so when a user is in a text box and they press enter, it is the same as clicking the link, in which case it should take them to another page. Here\'s what

7条回答
  •  遇见更好的自我
    2021-02-19 23:23

    Write a small jQuery plugin:

    jQuery.fn.enter = function(callback) {
       if(!callback) {
          //don't attach if we have garbage.
          return;
       }
    
       $(this).keydown(function(e) {
           var ev = e || event;
           if(ev.keyCode == 13) {
              callback();
              return false;
           }
       }); 
    };
    

    Usage: $(element).enter(callback_func);

    I hope this helps.

提交回复
热议问题