代码之家  ›  专栏  ›  技术社区  ›  scunliffe

当我在文本框中按Enter键时,我需要什么样的事件来阻止IE“丁丁”呢?

  •  8
  • scunliffe  · 技术社区  · 17 年前

    在只有一个文本框的简单表单上,按Enter提交表单(这对于轻松搜索表单很好)

    但是在表格上 倍数 字段,在input=“text”框中按Enter键不会执行任何操作(例如提交),但在IE中,它会“叮当”一声,就好像您试图删除一个不可删除的对象一样。

    问题是…我需要在IE中抑制什么事件来停止此声音?例如,如果我有一个用户名/密码表单,我确实希望Enter键提交表单,但我肯定不希望听到“错误”的声音。

    带声音的示例站点: http://www.sears.com/shc/s/StoreLocatorView?storeId=10153&catalogId=12605 只需在任何文本字段中按Enter。丁!丁!丁!

    非IE用户,声音为:程序事件>Windows>默认蜂鸣音(“windows xp ding.wav”)。

    5 回复  |  直到 12 年前
        1
  •  10
  •   scunliffe    17 年前

    好吧,看来这是可行的:

    <!-- suppress sound (and suppress submit) -->
    <input type="text" onkeypress="if(window.event.keyCode == 13){return false;}"/>
    
    <!-- suppress sound (BUT allow submit) -->
    <input type="text" onkeypress="if(window.event.keyCode == 13){this.form.submit();return false;}"/>
    
        2
  •  2
  •   anon    15 年前
    $("#logonForm").keypress(function (e) {
                if (e.keyCode == '13') {
                    if (instance.Validate(instance)) {
                        document.forms["logonForm"].submit();
                        return false;  //Do not delete, suppresses ding in IE... :P
                    }
                }
            });
    

    这行,我们用它。

        3
  •  0
  •   sth    16 年前

    他是对的。你想要的文本字段,不要用叮当声来烦你。

    只添加

    onkeypress="if(window.event.keyCode == 13){return false;}"
    

    给你输入标签

    <input type="whatever"  onkeypress="if(window.event.keyCode == 13){return false;}" >
    

    但不要在提交或按钮输入OK中执行此操作。只有文本字段,所以你不会被叮当声打扰!丁!现在。 我查过了。

        4
  •  0
  •   Code.Town    12 年前
    $("#txtSomething").keypress(function (e) {
            if (e.which == 13) {
    
                e.Handled = true; //This will prevent the "ding" sound
    
                //Write the rest of your code
            }
        });
    
        5
  •  -1
  •   shahkalpesh    17 年前

    我想这是由操作系统控制的,因此不能使用JavaScript控制。