为什么我的按钮失去焦点?

发布于 2024-09-27 07:54:07 字数 1496 浏览 1 评论 0原文

这就是我想要发生的事情。当用户点击“输入”时,应该触发“SearchForItems()”。发生的情况是页面上的另一个带有“其他按钮”的控件,只要在 onkeyup 之外或之后将任何其他元素切换到外面,焦点就会返回到该控件。

我必须在页面加载时强制将焦点集中在“搜索按钮”上,但之后任何时候按下 Enter 都会触发“其他按钮”。 “其他按钮”没有分配选项卡索引,是页面默认按钮

我不确定我是否已经很好地解释了这一点。基本上我想知道为什么焦点会转移以及如何强制“搜索按钮”在按下输入键时触发。

这是我页面上的一个条目,我试图强制触发“搜索按钮”:

<td><input type="text" onkeydown="CheckForEnterKey(event)" id="AcceptedDate1" maxlength="7"  style="width:121px;" value="<%=DateTime.Now.AddMonths(-3).ToString("MM/yyyy")%>"/>&nbsp;</td>

这是我的 jquery 文件的一部分,我也在其中使用

$('#AcceptedDate1').keypress(function(e) { if (e.keyCode == 13 || e.which == 13) $('#SearchAcceptedLotsButton').click(); });

CheckForEnterKey 函数

function CheckForEnterKey(e)
    {
        var keycode;
        if(e.which)
        {
            keycode = e.which;
        }
        else
        {
            keycode = e.keyCode;
        }

        if(keycode == 13)
        {
            $('#SearchAcceptedLotsButton').click();//tried SearchForItems() also
        }
    }

编辑: strong>

我刚刚将其添加到我的页面中,它似乎可以工作,我将其添加到将所有内容结合在一起的主 div 中。我现在需要做的是找出按下回车键的位置,以便每次在页面的主要部分中按下回车键时都会取消回车键。有什么想法吗?我的页面上有很多元素?

function checkKey(e)
    {
        if(e.keyCode == 13 || e.which == 13)
        {
            e.returnValue = false;
            e.cancel = true;
        }
    }

This is what I want to happen.. When a user hits 'enter' a 'SearchForItems()' should trigger. What is happening is another control, with 'other button', on the page gets focus returned to it anytime any other element is tabbed out of or after onkeyup.

I am having to force focus on the 'Search button' on page load, but after that any time enter is pressed the 'other button' is triggered. The 'other button' does not have a tab index assigned and is the pages default button.

I'm not sure if I've done a good job explaining this. Basically I want to know why focus is being shifted and how I can force the 'Search button' to trigger anytime enter is pressed.

this is one entry on my page where I'm trying to force the 'Search button' to trigger:

<td><input type="text" onkeydown="CheckForEnterKey(event)" id="AcceptedDate1" maxlength="7"  style="width:121px;" value="<%=DateTime.Now.AddMonths(-3).ToString("MM/yyyy")%>"/> </td>

this is part of my jquery file where i'm also

$('#AcceptedDate1').keypress(function(e) { if (e.keyCode == 13 || e.which == 13) $('#SearchAcceptedLotsButton').click(); });

CheckForEnterKey Function

function CheckForEnterKey(e)
    {
        var keycode;
        if(e.which)
        {
            keycode = e.which;
        }
        else
        {
            keycode = e.keyCode;
        }

        if(keycode == 13)
        {
            $('#SearchAcceptedLotsButton').click();//tried SearchForItems() also
        }
    }

Edit:

I just added this to my page and it appears to work, I added this to the main div thats holding everything together. What I need to do now is figure out where the enter was pressed so the enter is canceled everytime it is pressed within the main portion of my page. Any ideas, there are a lot of elements on my page?

function checkKey(e)
    {
        if(e.keyCode == 13 || e.which == 13)
        {
            e.returnValue = false;
            e.cancel = true;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七度光 2024-10-04 07:54:07

我不确定你为什么遇到问题。但我可以为您提供我的 onKeyDown 代码来按 Enter:

if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) { #DO JS HERE # }}

所以我这样使用它:

<input type="text" onKeyDown="if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) { alert('You pressed enter!'); }}">

I am not sure why you are having problems. But I can offer you my onKeyDown code for pressing enter:

if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) { #DO JS HERE # }}

So I use it like this:

<input type="text" onKeyDown="if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) { alert('You pressed enter!'); }}">
韵柒 2024-10-04 07:54:07

在上面的代码中,每当按下 Enter 时,id="SearchAcceptedLotsButton" 就会被触发点击,如下所示:

    if(keycode == 13)
    {
        $('#SearchAcceptedLotsButton').trigger('click');
    }

In the above code whenever enter is pressed the id="SearchAcceptedLotsButton" can be triggered click as follows :

    if(keycode == 13)
    {
        $('#SearchAcceptedLotsButton').trigger('click');
    }
飘然心甜 2024-10-04 07:54:07

这就是我解决问题的方法:

我添加了这个函数。

function checkKey(e)
    {
        if(e.keyCode == 13 || e.which == 13)
        {
            e.returnValue = false;
            e.cancel = true;
            SearchAcceptedLots();
        }
    }

在包含页面上所有内容的最顶部 div,我添加了这个:

<div id="ItemPanel" onkeydown="checkKey(event)">

这捕获了在表单中按下的每个输入,取消默认按钮行为并调用所需的函数。

this is how I resolved the issue:

I added this function..

function checkKey(e)
    {
        if(e.keyCode == 13 || e.which == 13)
        {
            e.returnValue = false;
            e.cancel = true;
            SearchAcceptedLots();
        }
    }

At the top most div containing everything on the page I added this:

<div id="ItemPanel" onkeydown="checkKey(event)">

this catches every enter that is pressed within the form, canceling the default buttons behavior and calling the desired function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文