.NET在Javascript onKeypress之前提交Fires

时间:2022-05-04 11:49:00

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.

我试图在输入类型=“文本”控件上使用onkeypress来触发一些javascript如果按下输入按钮。它适用于大多数页面,但我也有一些页面具有自定义.NET控件。

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

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

If it helps, here is my javascript:

如果有帮助,这是我的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;
}

3 个解决方案

#1


0  

How are you assigning the javascript?

你是如何分配javascript的?

It should look like:

它应该看起来像:

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

#2


0  

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..

Javascript OnKeyPress将始终首先触发,更多的是它是否已经在页面回发之前完成了它的操作。

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

我想重新考虑一下发生了什么以及在哪里......服务器端发生了什么?

#3


0  

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!

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

#1


0  

How are you assigning the javascript?

你是如何分配javascript的?

It should look like:

它应该看起来像:

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

#2


0  

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..

Javascript OnKeyPress将始终首先触发,更多的是它是否已经在页面回发之前完成了它的操作。

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

我想重新考虑一下发生了什么以及在哪里......服务器端发生了什么?

#3


0  

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!

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