简体   繁体   English

onKeypress在文本字段中输入Key事件在Chrome中不起作用

[英]onKeypress Enter Key event on textfield not working in Chrome

I have a question along the same vein as the one asked here regarding how the enter key is handled in chrome. 我有一个问题,就像这里提出的关于如何在chrome中处理回车键的问题一样。

The effect I am trying to accomplish is to allow the enter key to call a click event of one a button while focus in within the current field. 我想要完成的效果是允许输入键在焦点位于当前字段内时调用一个按钮的单击事件。 To accomplish this I am using the following code: 为此,我使用以下代码:

javascript: JavaScript的:

 <script type="text/javascript">
//attempting to capture keypress for chrome here but this is not working
        $("#txtContainer").keypress(function (e) {
            if (e.keyCode == '13') {
                e.preventDefault();
                doClick(buttonname, e);
                return false;
            }
        });

        function doClick(buttonName, e) {

            var key;
            if (window.event)
                key = window.event.keyCode;     //IE
            else
                key = e.which;     //firefox

            if (key == 13) {
                var btn = document.getElementById(buttonName);
                if (btn != null) { 
                    btn.click();
                    event.keyCode = 0
                } 
            }
        }
</script>

within the aspx 在aspx中

 <form id="form1" runat="server">
    <div>
    <asp:LinkButton ID="newBtn" runat="server" OnClick="btnLogin_Click" Text="ASP Link" />
    <asp:TextBox ID="txtContainer" runat="server" Width="100" />
    <asp:Label ID="time_lbl" runat="server" />
    </div>
    </form>

and within the code behind aspx.cs 并在aspx.cs后面的代码中

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtContainer.Attributes.Add("onKeyPress", "doClick('" + newBtn.ClientID + "',event)");
            }

        }       

        public void btnLogin_Click(object sender, EventArgs e)
        {
            time_lbl.Text = txtContainer.Text;
        }

The above code works fine in FF and IE however chrome continues to submit the entire form vs. capturing the keypress on the enterkey. 上面的代码在FF和IE中工作正常但是chrome继续提交整个表单而不是在enterkey上捕获按键。

Thank you for any suggestions. 谢谢你的任何建议。

我会说使用'keyup'而不是'keypress'可以解决你的问题

This should also do the job: 这也应该做的工作:

var key = e.keyCode || e.which;
if(key === 13 { /* your code */ }

You may try this in your keypress event 您可以在按键事件中尝试此操作

    $("#txtContainer").keypress(function (e) {
        e.preventDefault();
        var key = window.event ? e.keyCode : e.which;
        if (key == '13') {
            doClick(buttonname, e);
        }
    });

Note: var key = window.event ? 注意:var key = window.event? e.keyCode : e.which; e.keyCode:e.which; and e.preventDefault() should be placed at first and return false; 并且应该首先放置e.preventDefault()并返回false; is not required because you've used e.preventDefault(). 因为您使用了e.preventDefault(),所以不是必需的。

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

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