简体   繁体   English

按下回车时如何模拟tab和回车键,最好在JavaScript

[英]How to simulate tab and enter key when enter is pressed, preferably in JavaScript

I have found lots of solutions on how to replace enter with tab key, but I need to simulate tab and enter on enter, please help:我找到了很多关于如何用 tab 键替换 enter 的解决方案,但我需要模拟tab 并在 enter上输入,请帮助:

I have an input field and then a list of links (the list is filtered by the input).我有一个输入字段,然后是一个链接列表(该列表由输入过滤)。 When I'm done typing and press enter I want the first link to be "clicked".当我完成输入并按回车键时,我希望第一个链接被“点击”。 I assume one way i could do this is by simulating a tab key before the enter key, but how do i do that?我假设我可以做到这一点的一种方法是在回车键之前模拟一个制表键,但我该怎么做呢?

Are there any other alternatives?还有其他选择吗?

This is what i have:这就是我所拥有的:

    <input type="text" id="search_filter" style="COLOR: #999" onFocus="if (this.value == 'sometext') {this.value=''; this.style.color='#000';}" name="filter" value="someothertext" />
        <div id="links">    
            <ul>
                <li><a href="someurl">Link 1</a></li>
                <li><a href="someurl">Link 2</a></li>
                <li><a href="someurl">And so on</a></li>
            </ul>
        </div>

Thanks!谢谢!

<input type="text" id="input1"/>
<a href="someUrl">Link</a>

<script>

   $(function(){
           $("#input1").keypress(function(event){
                var ENTER = 13;
                if ( event.which == ENTER ) {
                     $(this).next("a").click();
                     e.preventDeafult();
                     return false;
                }
           })
   })
</script>

beware of the difference between keypress and keydown .注意keypresskeydown之间的区别。 Keypress won't fire on some keys. Keypress 不会触发某些键。 So here's my implementation with jQuery and keydown:所以这是我使用 jQuery 和 keydown 的实现:

editorField.keydown( function(e) {
        switch ( e.keyCode ) {
        //13 for Enter
        case 13:
            saveCell();
            break;

        case 40: //I don't remeber which one of those is 'tab'. 
        case 9: //The other should be right arrow
            cell.parent().next().find('td.value').dblclick();
            return false;
            break;
        default:
            break;
        }
    } );

this is my code for inline cell editing but hopefully you will adapt it to your needs.这是我的内联单元格编辑代码,但希望您能根据自己的需要进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM