简体   繁体   中英

Make enter key behave like tab key in Javascript

So far i have this being called on keypress.

function chkenter()
{
  e = event.keyCode;

  if (e == 13)
  {
    event.keyCode = 9;
    return false;
  }
}

but even though i set the keycode to 9 it doesn't tab over to the next control. How can i accomplish this? I'm working in Asp Classic.

What I ultimately want is for the focus to move to the next element. The next element's id, however, is not determined completely because its id was set in a loop, but at this point it exists. How can i set focus to the next control is the question.

It is an IE only script, so if you try to run it on firefox it wont work.

For it to work on IE, remove "return false;" from your code and dont forget to atach this function into "onkeydown" event.

You can do this using jQuery like so:

function checkKey(e){
    if( e.keyCode == 13 ){ // if enter key
        var e = jQuery.Event('keydown'); // create a manual event
        e.which = e.charCode = 0;
        e.keyCode = 9; // set the new event to "tab"
        $(this.target).trigger(e); // trigger the new event (on the document)
        return false;  // cancels the original "enter" event from executing
    }
}
// lets say you bind the event on the whole document...
$(document).on('keydown', checkKey);

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