.NET 在 Javascript onKeypress 之前提交触发
我正在尝试在输入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是如何分配 javascript 的?
它应该看起来像:
How are you assigning the javascript?
It should look like:
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?
这不是一个非常明确的问题,所以我会尝试一下 -
看起来您正在此处寻找“输入”按键。 问题似乎是“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!