在文本框中输入按键时,请避免发出哔声

时间:2022-12-09 20:49:23

When you create an aspx page as this one:

当您创建一个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>

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.

另一方面,我想处理enter onkeypress事件。

Tx!

8 个解决方案

#1


0  

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.

如果它与您的观众使用的浏览器相关,那么他们已经习惯了它,因为它发生在除您之外的其他所有网站上。我说它不值得担心 - 有点像关闭JavaScript的人。他们知道他们已经关闭了,他们已经习惯了那些缺乏某些功能的网站。你不能强迫它们打开它,你不能强迫它们关闭声音。这是其中一项经验,包括你必须接受网络应用程序。

#2


10  

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.

首先,当在网页上的文本框中按Enter键时,发出蜂鸣声并非标准行为。试试Google的搜索页面,或者就此而言,请尝试本页底部的“名称”,“电子邮件”和“主页”字段。在任何浏览器中按Enter键时都不会发出蜂鸣声。

To prevent the beep, handle the onKeyDown event on your <INPUT> tag and return false if the enter key is pressed:

要防止发出蜂鸣声,请处理标记上的onKeyDown事件,如果按下回车键则返回false:

<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):

这是使用jQuery的相同的事情(我实际测试了这个,它解决了我的问题):

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

我在这个帖子上找到了解决方案 - 感谢Tharn Jaggar:

http://codingforums.com/showthread.php?t=36491

#3


4  

A couple of things to note here...

这里有几点需要注意......

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

    如果隐藏提交按钮或根本没有提交按钮,则会发出警报。如果你像我通常那样手动滚动你自己的按钮(因为默认的OS按钮很难看),通常就是这种情况。您可能想要研究的一件事是使用

  2. The alert will still occur if you intercept keyCode 13 on keyup or keydown. I don't know what it is about these events, but you have to use keypress. This is what I usually do with jQuery on a global level...

    如果您在keyup或keydown上拦截keyCode 13,则仍会发出警报。我不知道这些事件是什么,但你必须使用按键。这就是我通常在全球范围内使用jQuery做的事情......

    $("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


1  

Put this in your form tag onkeypress="if(event.keyCode==13)return false;"

把它放在你的表单标签onkeypress =“if(event.keyCode == 13)return false;”

#5


0  

You have to turn it off in your sound preferences on your operating system. It's an Internet Explorer thing.

您必须在操作系统的声音首选项中将其关闭。这是Internet Explorer的事情。

#6


0  

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.

我在asp中没有做太多的事情,但我首先想到的是它类似于Winforms应用程序。我解决问题的第一步是将密钥(代码,密钥代码,事件中的任何名称)var设置为#0或null,以及在返回之前的事件调用中的任何变量(例如e.handled)。

#7


0  

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:

最后,我可以在文本字段或组合或任何组件中按Enter键同时禁用页面所有组件上的蜂鸣声。我做的是我在我的body标签中添加了[onkeypress =“if(event.keyCode == 13)return false;”],如下所示:

   <body onkeypress="if(event.keyCode==13)return false;">

I used above code in my GWT GXT Application.

我在GWT GXT应用程序中使用了上面的代码。

Krishan Babbar

#8


0  

KeyPressEvent.preventDefault() seems to do the trick in GWT

KeyPressEvent.preventDefault()似乎在GWT中做了这个技巧

#1


0  

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.

如果它与您的观众使用的浏览器相关,那么他们已经习惯了它,因为它发生在除您之外的其他所有网站上。我说它不值得担心 - 有点像关闭JavaScript的人。他们知道他们已经关闭了,他们已经习惯了那些缺乏某些功能的网站。你不能强迫它们打开它,你不能强迫它们关闭声音。这是其中一项经验,包括你必须接受网络应用程序。

#2


10  

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.

首先,当在网页上的文本框中按Enter键时,发出蜂鸣声并非标准行为。试试Google的搜索页面,或者就此而言,请尝试本页底部的“名称”,“电子邮件”和“主页”字段。在任何浏览器中按Enter键时都不会发出蜂鸣声。

To prevent the beep, handle the onKeyDown event on your <INPUT> tag and return false if the enter key is pressed:

要防止发出蜂鸣声,请处理标记上的onKeyDown事件,如果按下回车键则返回false:

<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):

这是使用jQuery的相同的事情(我实际测试了这个,它解决了我的问题):

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

我在这个帖子上找到了解决方案 - 感谢Tharn Jaggar:

http://codingforums.com/showthread.php?t=36491

#3


4  

A couple of things to note here...

这里有几点需要注意......

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

    如果隐藏提交按钮或根本没有提交按钮,则会发出警报。如果你像我通常那样手动滚动你自己的按钮(因为默认的OS按钮很难看),通常就是这种情况。您可能想要研究的一件事是使用

  2. The alert will still occur if you intercept keyCode 13 on keyup or keydown. I don't know what it is about these events, but you have to use keypress. This is what I usually do with jQuery on a global level...

    如果您在keyup或keydown上拦截keyCode 13,则仍会发出警报。我不知道这些事件是什么,但你必须使用按键。这就是我通常在全球范围内使用jQuery做的事情......

    $("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


1  

Put this in your form tag onkeypress="if(event.keyCode==13)return false;"

把它放在你的表单标签onkeypress =“if(event.keyCode == 13)return false;”

#5


0  

You have to turn it off in your sound preferences on your operating system. It's an Internet Explorer thing.

您必须在操作系统的声音首选项中将其关闭。这是Internet Explorer的事情。

#6


0  

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.

我在asp中没有做太多的事情,但我首先想到的是它类似于Winforms应用程序。我解决问题的第一步是将密钥(代码,密钥代码,事件中的任何名称)var设置为#0或null,以及在返回之前的事件调用中的任何变量(例如e.handled)。

#7


0  

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:

最后,我可以在文本字段或组合或任何组件中按Enter键同时禁用页面所有组件上的蜂鸣声。我做的是我在我的body标签中添加了[onkeypress =“if(event.keyCode == 13)return false;”],如下所示:

   <body onkeypress="if(event.keyCode==13)return false;">

I used above code in my GWT GXT Application.

我在GWT GXT应用程序中使用了上面的代码。

Krishan Babbar

#8


0  

KeyPressEvent.preventDefault() seems to do the trick in GWT

KeyPressEvent.preventDefault()似乎在GWT中做了这个技巧