.NET 在 Javascript onKeypress 之前提交触发

发布于 2024-07-10 04:41:59 字数 543 浏览 8 评论 0原文

我正在尝试在输入 type="text" 控件上使用 onkeypress 来在按下 Enter 按钮时触发一些 javascript。 它适用于大多数页面,但我也有一些带有自定义 .NET 控件的页面。

问题是 .NET 提交在 onkeypress 之前触发。 有人知道如何让 onkeypress 首先火吗?

如果有帮助,这是我的 JavaScript:

 function SearchSiteSubmit(myfield, e)
{
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else 
        return true;
    if (keycode == 13)
    {
        SearchSite();
        return false;
    }
    else 
        return true;
}

I am trying to use onkeypress on an input type="text" control to fire off some javascript if the enter button is pressed. It works on most pages, but I also have some pages with custom .NET controls.

The problem is that the .NET submit fires before the onkeypress. Does anybody have an insight on how to make onkeypress fire first?

If it helps, here is my javascript:

 function SearchSiteSubmit(myfield, e)
{
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else 
        return true;
    if (keycode == 13)
    {
        SearchSite();
        return false;
    }
    else 
        return true;
}

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

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

发布评论

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

评论(3

哑剧 2024-07-17 04:41:59

你是如何分配 javascript 的?

它应该看起来像:

<input id="TextID" type="text" onkeypress="return SearchSiteSubmit('TextID', event)" />

How are you assigning the javascript?

It should look like:

<input id="TextID" type="text" onkeypress="return SearchSiteSubmit('TextID', event)" />
黯淡〆 2024-07-17 04:41:59

Javascript OnKeyPress 总是会首先触发,这更多的是在页面回发之前它是否已经完成其操作的情况。

我想说重新考虑发生了什么以及在哪里......正在发生什么放置在服务器端?

Javascript OnKeyPress will always fire first, it's more a case of wether or not it has completed its operation before the page is posted back..

I would say rethink what is going on and where.. What is taking place at the server side?

淡淡的优雅 2024-07-17 04:41:59

这不是一个非常明确的问题,所以我会尝试一下 -

看起来您正在此处寻找“输入”按键。 问题似乎是“enter”键通常由浏览器自动绑定到表单上的提交按钮,这意味着当用户按 Enter 时,您提交表单,而不是运行此处的 javascript。 您应该做的是创建一个全局事件处理程序,检查按下 Enter 按钮时“MyField”是否具有焦点,如果是,则触发 javascript,而不是提交表单。 我希望我理解你的问题!

This isn't a very clear question so I'll give it a shot --

It looks like you're looking for a keypress of "enter" here. The problem seems to be that the "enter" key is usually bound to the submit button on a form automatically by the browser, which means that when the user presses enter, you submit the form, rather than running the javascript you have here. What you should do is make a global event handler that checks to see if "MyField" has the focus when the enter button is pressed, and if so, then fire the javascript, rather than submitting the form. I Hope I understood your question!

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