在文本框中输入按键时避免发出蜂鸣声
当您创建这样的 aspx 页面时:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
如何避免在文本框中按 Enter 键时发出蜂鸣声。
另一方面,我想处理 Enter onkeypress 事件。
TX!
When you create an aspx page as this one:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
how can you avoid the beep sound that happens when you are in a textbox and hit enter.
On the other hand I would like to handle the enter onkeypress event.
Tx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
首先,在网页上的文本框中按 Enter 键时发出蜂鸣声不是标准行为。 尝试 Google 的搜索页面,或者尝试此页面底部的“名称”、“电子邮件”和“主页”字段。 在任何浏览器中按 Enter 键时,它们都不会发出蜂鸣声。
为了防止发出蜂鸣声,请处理
标记上的 onKeyDown 事件,如果按下 Enter 键,则返回 false:
这里使用 jQuery 实现相同的效果(我实际上已经测试过这个并且它为我解决了问题):
我在这个线程上找到了解决方案 - 归功于 Tharn Jaggar:
http:// /codingforums.com/showthread.php?t=36491
First of all, it is NOT standard behavior to get a beep when pressing enter in a textbox on a webpage. Try Google's search page, or for that matter try the Name, Email, and Home Page fields at the bottom of this page. None of them beep when pressing enter - in any browser.
To prevent the beep, handle the onKeyDown event on your
<INPUT>
tag and return false if the enter key is pressed:And here is the same thing using jQuery (I've actually tested this and it solved the problem for me):
I found the solution on this thread - credit to Tharn Jaggar:
http://codingforums.com/showthread.php?t=36491
这里需要注意一些事情...
如果提交按钮被隐藏或根本没有提交按钮,则会出现警报。 如果您像我通常那样手动滚动自己的
按钮(因为默认的操作系统按钮很丑),通常会出现这种情况。 您可能需要研究的一件事是使用
因为您可以对它们进行比标准输入按钮更多的样式设置,并且它们将处理按键事件有机地发生。
如果您在
keyup
或keydown
上拦截 keyCode 13,警报仍然会发生。 我不知道这些事件是什么,但你必须使用keypress
。 这就是我通常在全球范围内使用 jQuery 所做的事情...然后在页面级别我捕获提交事件并执行我的验证例程...
A couple of things to note here...
The alert will occur if the submit button is hidden or if there is no submit button at all. This is often the case if you hand-roll your own
<a>
buttons like I usually do (because default OS buttons are ugly). One thing you may want to look into is using<button type="submit"></button>
because you can style them a lot more than standard input buttons and they will handle the key events organically.The alert will still occur if you intercept keyCode 13 on
keyup
orkeydown
. I don't know what it is about these events, but you have to usekeypress
. This is what I usually do with jQuery on a global level...And then on the page level I trap the submit event and do my validation routines...
将其放入表单标签
onkeypress="if(event.keyCode==13)return false;"
Put this in your form tag
onkeypress="if(event.keyCode==13)return false;"
如果它与您的受众使用的浏览器有关,那么他们就会习惯这种情况的发生,因为除了您的网站之外,它也会发生在其他所有网站上。 我想说这不值得担心——有点像人们关闭了 JavaScript。 他们知道自己已经将其关闭,并且因此习惯了网站缺乏某些功能。 你不能强迫他们打开它,也不能强迫他们关闭声音。 这是您在网络应用程序中必须接受的体验之一。
If it's related to the browser that your audience is using, then they're used to it happening, because it happens on every other site besides yours. I'd say it's not worth worrying about - kind of like people with JavaScript turned off. They know they've got it turned off, and they're accustomed to sites lacking certain functionality as a result. You can't force them to turn it on, and you can't force them to turn the sound thing off. It's one of those experience comprises you've got to accept in web apps.
您必须在操作系统的声音首选项中将其关闭。 这是 Internet Explorer 的事情。
You have to turn it off in your sound preferences on your operating system. It's an Internet Explorer thing.
我没有在 asp 中做过太多事情,但我的第一个想法是它会类似于 Winforms 应用程序。 我解决这个问题的第一步是将键(代码、键代码,无论事件中的名称)var 设置为 #0 或 null,以及返回之前在事件调用中设置任何变量,例如 e.handled。
I haven't done much of anything in asp, but my first thought is that it would be similar to a Winforms application. My first step in solving it would be to set the key (code, keycode, whatever it's named in the event) var to #0 or null, as well as any vars such as e.handled in the event call prior to returning.
最后,我可以在文本字段或组合或任何组件中按 Enter 键时禁用页面所有组件上的蜂鸣声,这是必需的。 我所做的是在我的 body 标记中添加了 [onkeypress="if(event.keyCode==13)return false;"] ,如下所示:
我在 GWT GXT 应用程序中使用了上面的代码。
克里尚·巴巴尔
Finally I am able to disable the beep sound on all the components of the page while pressing Enter key in textfield or combo or any component, which was required. What I did is I added [onkeypress="if(event.keyCode==13)return false;"] in my body tag like given below:
I used above code in my GWT GXT Application.
Krishan Babbar
KeyPressEvent.preventDefault()
似乎可以在 GWT 中解决问题KeyPressEvent.preventDefault()
seems to do the trick in GWT