HTML - 按文本字段中的Enter键设置焦点在其他文本字段上

时间:2015-04-25 19:48:04

标签: html

我想用HTML创建一个带有3个文本字段和1个按钮的表单。在加载页面或刷新txt1文本字段时将自动聚焦,当按下回车键时,txt2将被聚焦,如果再次按下,则txt3将被聚焦,当再次按下时,它将提交。

有人知道怎么做吗?请帮忙......下面的代码示例......

谢谢......



<form name="form1" action="filename.php" method="post">
text1: <input type="text" name="txt1" autofocus><br>
text2: <input type="text" name="txt2"><br>
text3: <input type="text" name="txt3><br>
<input type="submit">
</form>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您可以使用HTML和jQuery的组合来完成此任务:

HTML:

var i18n = require("i18n");

i18n.configure({
    locales: ['en-US', 'fr-FR'],
    defaultLocale: 'fr-FR',
    directory: __dirname + '/locales',
    cookiename: 'locale'
});

app.get('/:locales/about', function(req, res) {
    var language = req.params.language;
     res.cookie('locale', language); 
    i18n.setLocale(language);
   res.redirect('/');
});

jQuery的:

<form name="form1" action="filename.php" method="post">
    text1: <input type="text" id="txt1" name="txt1" autofocus><br>
    text2: <input type="text" id="txt2" name="txt2"><br>
    text3: <input type="text" id="txt3" name="txt3"><br>
        <input type="submit" />
</form>

演示:https://jsfiddle.net/s4r6a1qo/3/

答案 1 :(得分:0)

<script>
function runScript(e) {
    //See notes about 'which' and 'key'
    if (e.keyCode == 13) {        
        document.getElementById("scriptBox2").focus();
        return false;
    }
}
</script>
<input id="scriptBox" type="text" onkeypress="return runScript(event)" />
<input id="scriptBox2" type="text" />