简体   繁体   English

Enter在Internet Explorer和Firefox中不起作用

[英]Enter is not working in Internet explorer and Firefox

This function is not working in Internet Explorer and Firefox. 此功能在Internet Explorer和Firefox中不起作用。 In Firefox, it returns the following error: 在Firefox中,它返回以下错误:

TypeError: e is undefined TypeError:e未定义
[Break On This Error] [打破此错误]
...e.which == "number") ? ... e.which ==“数字”)? e.which : e.keyCode; e.which:e.keyCode; keyCodeEntered = (e.which) ? keyCodeEntered =(e.which)? e.which... e.which ...

The function: 功能:

function onSearchPhraseKeyDown(e) {
    var buttonId = '<%=SearchButton.ClientID %>';
        if (!e)
            var e = (typeof e.which == "number") ? e.which : e.keyCode;
        keyCodeEntered = (e.which) ? e.which : window.event.keyCode;

        if ((keyCodeEntered == 13) || (keyCodeEntered == 13)) {
            document.getElementById(buttonId).click();
            return false;
        } else {
            return true;
        }
}

Can anyone see what's wrong? 任何人都可以看到什么是错的?

Try this code: 试试这段代码:

function onSearchPhraseKeyDown(e) {
    var buttonId = '<%=SearchButton.ClientID %>';
    var e = e || event;
    var key = e.keyCode || e.which;
    if (key===13) {
        document.getElementById(buttonId).click();
        return false;
    }
}

If you are using return false to cancel the default behaviour of this event, I suggest you to use e.preventDefault() instead, like this: 如果您使用return false取消此事件的默认行为,我建议您使用e.preventDefault() ,如下所示:

if (key===13) {
    document.getElementById(buttonId).click();
    e.preventDefault();
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script>
function checkEnter(event) {
if(event && event.which && event.which == 13) {

    alert("enter pressed");
}else if(event && event.keyCode && event.keyCode == 13) {

    alert("enter pressed");

}
return true;
}
</script>
<BODY  >
<div contenteditable id="temp"  style ="height:220px;background:white;width:545px;text-align:left;overflow:auto;margin:0px;border:solid 1px #999999;font-family:Arial;font-size: 10pt;color: black;" onKeyPress="return checkEnter(event)">

</BODY>
</HTML>

Please try this 请试试这个

Here is another option based on what I think that you are trying to accomplish: 这是另一种基于我认为你想要完成的选择:

If you are trying to make a form postback when a user presses enter while in some input field, you can use the DefaultButton property of either the panel control or the HtmlForm. 如果在用户在某些输入字段中按Enter键时尝试进行表单回发,则可以使用面板控件或HtmlForm的DefaultButton属性。 Here is an example using the HtmlForm.DefaultButton property: 以下是使用HtmlForm.DefaultButton属性的示例:

Default.aspx Default.aspx的

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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></title>
</head>
<body>
    <form id="form1" runat="server" defaultbutton="SubmitButton">
    <div>
        <asp:TextBox ID="InputTextBox" runat="server" />
        <asp:Button ID="SubmitButton" runat="server" Text="Submit" />
        <asp:Label ID="InputValue" runat="server" />
    </div>
    </form>
</body>
</html>

Default.aspx.vb Default.aspx.vb

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub SubmitButton_Click(sender As Object, e As System.EventArgs) Handles SubmitButton.Click
        InputValue.Text = InputTextBox.Text
    End Sub
End Class

Run this example and the label will contain the value of the textbox as an easy way to prove that the postback is occuring. 运行此示例,标签将包含文本框的值,作为证明回发发生的简单方法。 There are a few caveats to this approach, namely that the target of the DefaultButton property must be either a Button or ImageButton. 这种方法有一些注意事项,即DefaultButton属性的目标必须是Button或ImageButton。 LinkButton is not allowed. 不允许使用LinkBut​​ton。 More info at the following links: 更多信息,请访问以下链接:

system.web.ui.webcontrols.panel.defaultbutton system.web.ui.webcontrols.panel.defaultbutton

system.web.ui.htmlcontrols.htmlform.defaultbutton system.web.ui.htmlcontrols.htmlform.defaultbutton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 幻灯片只能在Firefox中使用,而不能在Internet Explorer中使用 - slideshow working in firefox but not internet explorer Jquery在Firefox中工作但不在Internet Explorer中工作 - Jquery working in Firefox but not Internet Explorer FadeIn不适用于FireFox / Internet Explorer - FadeIn not working for FireFox/Internet Explorer 内部超链接在Firefox和Internet Explorer中不起作用 - internal hyperlink not working in firefox and internet explorer this.value在Firefox上运行,但在Internet Explorer中不运行? - this.value is working on Firefox, but not in Internet Explorer? 元标记中的javascript在Firefox和Internet Explorer上不起作用 - javascript in meta tag not working on firefox and internet explorer 为什么这在fireFox中有效,但在Chrome或Internet Explorer 9中却无效? - Why is this working in fireFox but not in Chrome or Internet Explorer 9? jQuery滚动在Internet Explorer或Firefox中不起作用 - JQuery scrolling not working in Internet Explorer or Firefox 图像淡入淡出不适用于 Internet Explorer 或 Firefox - Image fadeIn not working with Internet Explorer or Firefox Angular-自定义元素在Firefox&Microsoft Edge&Internet Explorer上不起作用 - Angular - Custom Elements not working on Firefox & Microsoft Edge & Internet Explorer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM