简体   繁体   中英

How to simulate TAB on ENTER keypress in javascript or jQuery

I want to change keycode in keydown ( key press ) in all input in a page.I want to replace Enter keycode with TAB key code. How I can do this?

thanks


EDIT 1)

Consider this code:

<div>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
    </asp:RadioButtonList>
    <br />
    <br />
    <asp:TextBox ID="TextBox1" runat="server">3333</asp:TextBox>
    <br />
    <br />
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:DropDownList>
</div>

I want when user press Enter on eny of above control focus go to next control.

thanks

I've had a similar problem, where I wanted to press + on the numpad to tab to the next field. Now I've released a library that I think will help you.

PlusAsTab : A jQuery plugin to use the numpad plus key as a tab key equivalent.

Since you want enter / instead, you can set the options. Find out which key you want to use with the jQuery event.which demo .

JoelPurra.PlusAsTab.setOptions({
  // Use enter instead of plus
  // Number 13 found through demo at
  // https://api.jquery.com/event.which/
  key: 13
});

Then enable the feature by adding plus-as-tab="true" to the form fields you want to use enter-as-tab in, or some other element that contains these form fields. Radio buttons should not be a problem, as they are covered by my other library, EmulateTab - see autonavigation of radio buttons in that demo .

<div plus-as-tab="true">
  <!-- all focusable elements inside the <div> will be enabled -->
  <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <!-- Radio buttons should not be a problem. -->
  </asp:RadioButtonList>
</div>

You can try it out yourself in the PlusAsTab enter as tab demo .

This code is to replace enter with tab character:

$("#wmd-input").bind("keypress", function(e) {
   if (e.keyCode == 13) {
      var input = $(this);
      var inputVal = input.val();
      setTimeout(function() {
        input.val(inputVal.substring(0,inputVal.length) + "\t");
      }, 1);
   }
});

Live Demo

UPDATE:

This code is to focus to on the next element:

$(document).ready(function () {
    $("input,select").bind("keydown", function (e) {
        if (e.keyCode == 13) {
            var allInputs = $("input,select");
            for (var i = 0; i < allInputs.length; i++) {
                if (allInputs[i] == this) {
                    while ((allInputs[i]).name == (allInputs[i + 1]).name) {
                        i++;
                    }

                    if ((i + 1) < allInputs.length) $(allInputs[i + 1]).focus();
                }
            }
        }
    });
});

Hope this works

$('input,textarea').keydown(function(){
  if(event.keyCode==13) {
    event.keyCode = 9;
  }
});

Edit

Try this http://jsfiddle.net/GUmUg/ . Play around with selectors to make this work as i don't know asp

$('input,textarea').keypress(function(e){
    if(e.keyCode==13) {
   $(this).next().focus();
  }
});
$('input').on('keydown',function(e){
    var keyCode = e.keyCode || e.which;
     if(e.keyCode === 13) {
      e.preventDefault();
      $('input')[$('input').index(this)+1].focus();
      }
});

check fiddle here : http://jsfiddle.net/Pd5QC/

The way I do it is by using jquery to each over your selection and focusing on the element after the current on you are on.

$(document).on('keyup', '.my-input', function (ev) {

    if (ev.keyCode == '13') {
        var currentInput = this;
        var isOnCurrent = false;

        $('.my-input').each(function () {

            if (isOnCurrent == true) {
                $(this).focus();
                return false;
            }

            if (this == currentInput) {
                isOnCurrent = true;
            }

        });
    }
});

I created a simple jQuery plugin which does solve this problem. It uses the ':tabbable' selector of jQuery UI to find the next 'tabbable' element and selects it.

Example usage:

// Simulate tab key when enter is pressed           
$('.myElement').bind('keypress', function(event){
    if(event.which === 13){
        if(event.shiftKey){
            $.tabPrev();
        }
        else{
            $.tabNext();
        }
        return false;
    }
});
$(document).ready(function() {

//Objetos con CssClass="EntTab" sustituye el Enter (keycode 13) por un Tabulador (keycode 9)!!

    $(".EntTab").bind("keypress", function(e) {
        if (e.keyCode == 13) {
            var inps = $("input, select"); //add select too
            for (var x = 0; x < inps.length; x++) {
                if (inps[x] == this) {
                    while ((inps[x]).name == (inps[x + 1]).name) {
                        x++;
                    }
                    if ((x + 1) < inps.length) $(inps[x + 1]).focus();
                }
            } e.preventDefault();
        }
    });
});

I think this work:

 $('input').live("keypress", function (e) {
        /* ENTER PRESSED*/
        var OffSet = 0;
        if (e.keyCode == 13) {
            /* FOCUS ELEMENT */
            if ($(this).is("input[type='radio']")) {
                var tblID = $(this).closest('table').attr('id');
                var radios = $('#' + tblID).find(":input");
                //alert(radios.index(this));
                OffSet = radios.length - radios.index(this) - 1;
            }
            //alert(OffSet);

            var inputs = $(this).parents("form").eq(0).find(":input");
            var idx = inputs.index(this);
            inputs[idx + OffSet].blur();

            try {
                inputs[idx + OffSet].selectionStart = inputs[idx + OffSet].selectionEnd = -1;
            } catch (e) {

            }
            if (idx == inputs.length - 1) {
                inputs[0].select();
            } else {
                inputs[idx + 1 + OffSet].focus(); //  handles submit buttons
                try {
                    inputs[idx + 1 + OffSet].select();
                } catch (e) {
                }
            }
            return false;
        }
    });

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