问题
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!
回答1:
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.
回答2:
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:
<input type="text" name="TextBox1" value="" onKeyDown="return StopBeepOnEnter(event)" />
<script type="text/javascript">
function StopBeepOnEnter(event)
{
if (event.keyCode == 13)
{
// Do Whatever...
return false; // This stops the beep
}
}
</script>
And here is the same thing using jQuery (I've actually tested this and it solved the problem for me):
<script type="text/javascript">
$("#<%=TextBox1.ClientID %>").keydown(function(event)
{
if (event.keyCode == 13)
{
// Do Whatever...
return false; // This stops the beep
}
});
</script>
I found the solution on this thread - credit to Tharn Jaggar:
http://codingforums.com/showthread.php?t=36491
回答3:
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...$("input[type=text],input[type=password]").keypress(function(e) { if(e.keyCode == 13) { $(this).closest("form").trigger("submit"); e.preventDefault(); } });
And then on the page level I trap the submit event and do my validation routines...
$("form").submit(function(e) { var txt = $("#txtUser"); if(txt.val().length == 0) { alert("Who are you?"); txt.focus(); e.preventDefault(); } });
回答4:
Put this in your form tag onkeypress="if(event.keyCode==13)return false;"
回答5:
You have to turn it off in your sound preferences on your operating system. It's an Internet Explorer thing.
回答6:
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.
回答7:
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:
<body onkeypress="if(event.keyCode==13)return false;">
I used above code in my GWT GXT Application.
Krishan Babbar
回答8:
KeyPressEvent.preventDefault()
seems to do the trick in GWT
来源:https://stackoverflow.com/questions/441185/avoid-beep-sound-when-enter-keypress-in-a-textbox