【问题标题】:How to convert enter key press to tab key press, without submitting form如何在不提交表单的情况下将 Enter 按键转换为 Tab 按键
【发布时间】:2013-11-23 11:03:07
【问题描述】:

我尝试在不提交表单的情况下将输入键转换为选项卡,但只需按 Enter 即可删除提交表单...

观点:

 <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>

脚本:

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

        e.preventDefault();
    }
}

【问题讨论】:

  • 您要更改回车键的默认功能吗?
  • 是的,我的客户想要一个类似 excel 的表格
  • 使用这个? if(event.keyCode==13){event.keyCode=9; return event.keyCode}??

标签: javascript html asp.net-mvc model-view-controller form-submit


【解决方案1】:

您可以通过以下 javascript 来完成:

<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>

不要使用onkeypress() 功能,只需将其删除即可。

【讨论】:

    【解决方案2】:

    而不是

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

    更好的使用

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

    因为 val() 返回输入字段的内容。输入“提交”并在输入时提交内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多