如何防止HTML输入字段中的键UP和DOWN行为?

时间:2021-08-19 00:09:44

I know there are similar questions on *, but none of them I think has working solution. So when you type key UP or Down when you have focus on an HTML input field, the cursor automatically moves to the front/end of the input value.

我知道*上有类似的问题,但我认为它们都没有工作解决方案。因此,当您关注HTML输入字段时键入UP或Down键时,光标会自动移动到输入值的前端/末尾。

(You can check this with the Top-right Search box on the * site).

(您可以使用*站点上的右上角搜索框进行检查)。

I want to remove this!!

我想删除这个!!

I tried the following code, but didn't work:

我尝试了以下代码,但没有奏效:

$(document).on("keydown", "#input_field", function(event) {
    if(event.which==38 || event.which==40){
        event.preventDefault();
    }
});

Any solution for this..?

任何解决方案..?

1 个解决方案

#1


5  

I don't see the point in preventing a fairly useful behavior, but this works for me with the SO search box:

我没有看到防止一个相当有用的行为的意义,但这对我来说是SO搜索框:

$(document).on("keydown keyup", "#search input", function(event) { 
    if(event.which==38 || event.which==40){
        event.preventDefault();
    }
});

This code targets both keyup and keydown events.

此代码针对keyup和keydown事件。

#1


5  

I don't see the point in preventing a fairly useful behavior, but this works for me with the SO search box:

我没有看到防止一个相当有用的行为的意义,但这对我来说是SO搜索框:

$(document).on("keydown keyup", "#search input", function(event) { 
    if(event.which==38 || event.which==40){
        event.preventDefault();
    }
});

This code targets both keyup and keydown events.

此代码针对keyup和keydown事件。