简体   繁体   中英

Enter is not working in Internet explorer and Firefox

This function is not working in Internet Explorer and Firefox. In Firefox, it returns the following error:

TypeError: e is undefined
[Break On This Error]
...e.which == "number") ? e.which : e.keyCode; keyCodeEntered = (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:

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. Here is an example using the HtmlForm.DefaultButton property:

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

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. LinkButton is not allowed. More info at the following links:

system.web.ui.webcontrols.panel.defaultbutton

system.web.ui.htmlcontrols.htmlform.defaultbutton

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM