简体   繁体   中英

Getting key code in Firefox from webforms textbox keydown event

My textbox is declared thus:

<asp:TextBox ID="txtShowManyItemTag" CssClass="price" onkeydown="return txtShowManyItemTagKeyUp(this);" TabIndex= "997" runat="server" Text="" Width="50px"   /> 

The javascript function called is:

function txtShowManyItemTagKeyUp(txt) {
    if (window.event.keyCode == 13 && txt.value != '') {
        var nextRow = $(txt).closest('tr').next();
        if (nextRow.length > 0) {
            $(txt).closest('tr').next().find(".price").select();
        }
        else {
            $("#<%=btnOkMany.ClientID %>").select();
        }

        return false;
    }
}

In Chrome and IE, the window.event.keyCode == 13 correctly detects the Enter key being pressed, but I have been unable to find the equivalent for Firefox. Note that I am not passing an event, I'm passing the control that's triggering the event, and I can find no way to get the key code from that object. I'm going through stack overflow, but have not yet found something that matches this situation.

Thanks!

Instead of the onkeydown attribute, use

$("#txtShowManyItemTag").on('keydown', txtShowManyItemTagKeyUp);

And declare the function as

function txtShowManyItemTagKeyUp(e) { ... }

Inside it, you can now use e to refer to the event, and this to refer to the <input> .

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