最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert enter key press to tab key press, without submitting form - Stack Overflow

programmeradmin4浏览0评论

I try to convert enter key press to tab without submitting form, but just can remove submitting form pressing enter...

The view:

 <tr>
    <td><input type="text" id="tipoContacto1" name="tipoContacto1" class="form-control input-sm" onkeypress="removerEnter(event)"/></td>
    <td><input type="text" id="nomeContacto1" name="nomeContacto1" class="form-control input-sm" onkeypress="removerEnter(event)"/></td>

The script:

function removerEnter(e) {
    if (e.which == 13) {
        //$(e).target.nextSibling;
        //$(this).next('input').focus();

        e.preventDefault();
    }
}

I try to convert enter key press to tab without submitting form, but just can remove submitting form pressing enter...

The view:

 <tr>
    <td><input type="text" id="tipoContacto1" name="tipoContacto1" class="form-control input-sm" onkeypress="removerEnter(event)"/></td>
    <td><input type="text" id="nomeContacto1" name="nomeContacto1" class="form-control input-sm" onkeypress="removerEnter(event)"/></td>

The script:

function removerEnter(e) {
    if (e.which == 13) {
        //$(e).target.nextSibling;
        //$(this).next('input').focus();

        e.preventDefault();
    }
}
Share Improve this question edited Nov 11, 2013 at 15:39 ajc 1,73514 silver badges36 bronze badges asked Nov 11, 2013 at 15:22 CesarMiguelCesarMiguel 3,8304 gold badges25 silver badges34 bronze badges 4
  • Do you want change the default functionality of enter key? – ajc Commented Nov 11, 2013 at 15:28
  • yes, my client wants a form like excel – CesarMiguel Commented Nov 11, 2013 at 15:29
  • stackoverflow./questions/1009808/… – ajc Commented Nov 11, 2013 at 15:32
  • using this? if(event.keyCode==13){event.keyCode=9; return event.keyCode} ?? – CesarMiguel Commented Nov 11, 2013 at 15:42
Add a ment  | 

2 Answers 2

Reset to default 6

You can do by the following javascript which are as under:

<script type="text/javascript">
    $(document).ready(function () {
        $("input").not($(":button")).keypress(function (evt) {
            if (evt.keyCode == 13) {
                iname = $(this).val();
                if (iname !== 'Submit') {
                    var fields = $(this).parents('form:eq(0),body').find('button, input, textarea, select');
                    var index = fields.index(this);
                    if (index > -1 && (index + 1) < fields.length) {
                        fields.eq(index + 1).focus();
                    }
                    return false;
                }
            }
        });
    });
</script>

Don't use onkeypress() function just remove it.

Instead of

iname = $(this).val();
if (iname !== 'Submit')..

better use

itype = $(this).attr('type');
if (itype !== 'submit')..

because val() returns the content of the input-field. Type 'Submit' and the content is submitted on enter.

发布评论

评论列表(0)

  1. 暂无评论