改变输入从提交到标签? [英] Change Enter from submission to Tab?

查看:92
本文介绍了改变输入从提交到标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户不喜欢的事实,回车键提交页面。所以我任务是preventing提交和改变回车键到Tab键切换到下一个字段。

Users don't like the fact that the Enter key submits the page. So I am tasked with preventing the submission and changing the Enter key to a Tab to the next field.

我曾尝试在网上发现了许多的JavaScript片段,但到目前为止都没有奏效。唯一一个甚至已经接近有一个效果即jQuery的API,它停止提交preventDefault(),但没有我曾尝试模仿标签的行为。

I have tried many javascript snippets found on the net but none have worked so far. The only one that has even come close to having an effect was e.preventDefault() of the jQuery API, which stops the submit, but nothing I have tried emulates the tab behavior.

e.returnValue = false;
e.cancel = true;

页仍与上述提交的keydown事件处理程序。与同样的效果返回在keydown事件处理虚假。该处理器被解雇,通过把一个断点它与萤火虫测试。

Page still submits with the above in the keydown event handler. Same effect with return false in the keydown event handler. The handler is firing, tested by putting a breakpoint in it with firebug.

这需要既IE和Firefox的工作。

This needs to work with both IE and Firefox.

不要说不这样做。结果
1)我已经确信,我不应该这样做,但它不是一个选择,那就是我的,所以讨论的是静音。结果
2)这将是一个问题的答案:我应该这样做?,这是不是我问的问题。

Don't say "don't do this".
1) I'm already convinced that I shouldn't do it, but it's not a choice that is mine, so the discussion is mute.
2) It would be an answer to the question "Should I do this?", which is not the question that I am asking.

推荐答案

这只是感觉恶心,但你可以使用事件。preventDefault 正如你所提到,然后叫焦点()上的下一个最接近的输入:

This just feels icky, but you could use event.preventDefault as you mentioned and then call focus() on the next closest input:

下面是一个简单的例子:

Here's a simple example:

$("input").bind("keydown", function(event) {
    if (event.which === 13) {
        event.stopPropagation();
        event.preventDefault();
        $(this).next("input").focus();
    }
});

例如: http://jsfiddle.net/andrewwhitaker/Txg65/

更新:如果您在你的输入之间的元素,使用普通的的next()将不起作用。相反,使用 nextAll()

Update: If you have elements in between your inputs, using plain next() will not work. Instead, use nextAll():

$("input").bind("keydown", function(event) {
    if (event.which === 13) {
        event.stopPropagation();
        event.preventDefault();
        $(this).nextAll("input").eq(0).focus();
    }
});

http://jsfiddle.net/andrewwhitaker/GRtQY/

这篇关于改变输入从提交到标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆