1

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();
    }
}
ajc
  • 1,685
  • 14
  • 34
CesarMiguel
  • 3,756
  • 4
  • 24
  • 34

2 Answers2

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.

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
Vaibhav Parmar
  • 645
  • 4
  • 11
0

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.

franxic
  • 1
  • 3