1

可能重复:
防止使用回车键提交表单

在我的项目中,我试图禁用该Enter键并提供我自己的输入键功能。(我还没有做过

下面的功能正在工作,但是每当我Enter在字段内按下键时TextBox,内容就会添加到div(根据需要)并且在TextBox字段中,输入键功能正在发生(我不想要)。如何停止输入键功能在TextBox字段上运行?为了清楚地理解,请参阅我在下面代码中的评论。

.aspx 文件工作

<asp:TextBox ID="msg" BackColor="Transparent" runat="server" BorderStyle="None"
                        TextMode="MultiLine" />

jQuery工作

$('#msg').keypress(function (e) {
            if (e.which == 13) {

                //Shows the TextBox text in a Div, which I want to.
                chat.server.send($('#msg').val());  //also going one step down in TextBox field which I dont want to.

                $('#msg').val(''); //Clearing the Text in TextBox field

                //what should I add here to make the Enter Key work only for the DIV?
            }
        });

来源

4

2 回答 2

4

试试e.preventDefault() 这样

$('#msg').keypress(function (e) {
            if (e.which == 13) {
                e.preventDefault();
                //Shows the TextBox text in a Div, which I want to.
                chat.server.send($('#msg').val());  //also going one step down in TextBox field which I dont want to.

                $('#msg').val(''); //Clearing the Text in TextBox field

                //what should I add here to make the Enter Key work only for the DIV?
            }
        });
于 2012-11-26T07:17:36.463 回答
0

也尝试使用 C# 在 C# 中进行验证。只需编写以下函数并调用提供参数的函数(如下所述)

public void disable_TextBox_Enter(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if ((c.Controls.Count > 0))
            {
                disable_TextBox_Enter(c);
            }
            else
            {
                if (c is TextBox)
                {
                    ((TextBox)(c)).Attributes.Add("onkeydown", "return (event.keyCode!=13);");

                }
                if (c is GridView)
                {

                    ((GridView)(c)).Attributes.Add("onkeydown", "return (event.keyCode!=13);");

                }

            }
        }
    }

你可以像这样调用函数:

disable_TextBox_Enter(this);
于 2012-11-26T11:30:20.497 回答