javascript 按下回车键后jQuery焦点退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18758469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:07:16  来源:igfitidea点击:

Jquery focus out after pressing enter key

javascriptphpjqueryhtmlcss

提问by Friend

My code is below:

我的代码如下:

$('.summaryT').keypress(function(e){
    if(e.which == 13){
        callajax();
        $(this).focusout();

    }
});

As you can see on the code above, When a user presses the enter key first callajax()is run(working fine). After that I want to focus out from the .summaryTinput box, How can I achieve this?

正如您在上面的代码中看到的那样,当用户首先按下 Enter 键时callajax()运行(工作正常)。之后我想从.summaryT输入框中聚焦出来,我怎样才能做到这一点?

回答by Bibhu

Try this

试试这个

$('.summaryT').keypress(function(e){
    if(e.which == 13){
        callajax();
        $(this).blur();    
    }
});

回答by fabrik

Since AJAX stands for asynchronous, you may want to call focusout()after the call successfully finished.

由于 AJAX 代表异步,您可能希望focusout()在调用成功完成后调用。

回答by Liam Allan

use the jquery blur()event

使用 jquery blur()事件

$('.summaryT').keypress(function(e){
    if(e.which == 13){
        callajax();
        $(this).blur();

    }
});

回答by ashish patel

Here is the solution even if Mousetrap is activated

即使激活捕鼠器,这也是解决方案

    $('.selectorClass').keypress(function(e)
    {
        if(e.which == 13)
        {
            // 13 is a code to hit keyboard enter button
            alert('Enter is pressed');
        }
    });

    $('#selectorID').keypress(function(e)
    {
        if(e.which == 13)
        {
            // 13 is a code to hit keyboard enter button
            alert('Enter is pressed');
        }
    });