javascript 防止回发在 asp.net 输入字段中输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18639497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:42:27  来源:igfitidea点击:

Prevent postback on Enter in a asp.net inputfield

c#javascriptasp.netpostbackenter

提问by Maximus Decimus

I have a problem with the key Enter in javascript and asp.net

我在 javascript 和 asp.net 中输入键有问题

I have a control like this with a textchanged event which does a find but I want to control it when the user does enter

我有一个这样的控件,它带有一个 textchanged 事件,它可以进行查找,但我想在用户输入时控制它

<asp:TextBox ID="TextBox1" runat="server" onkeyup="EnterEvent(event)" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" />

That's why I created this javascript function which works very well. Because it avoids the Enter postback at any character input

这就是为什么我创建了这个运行良好的 javascript 函数。因为它避免了在任何字符输入时的 Enter 回发

    function EnterEvent(e) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if (keycode == 13) {
            return true;
        }
        else {
            return false
        }
    }

And then I wanted to control when the TextBox has content, so I changed the js like this.

然后我想控制TextBox什么时候有内容,所以我把js改成这样。

    function EnterEvent(e, ctrl) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if (keycode == 13) {
            return ctrl.value.length > 2;
        }
        else {
            return false
        }
    }

The control

控制

<asp:TextBox ID="TextBox1" runat="server" onkeyup="EnterEvent(event, this)" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" />

But nothing happens. Anytime that I do Enter, the page postback.

但什么也没有发生。每当我输入时,页面回发。

I also added this in the code behind on the load page

我还在加载页面后面的代码中添加了这个

            TextBox1.Attributes.Add("onkeypress", "EnterEvent(event, this);");
            TextBox1.Attributes.Add("onkeyup", "EnterEvent(event, this);");
            TextBox1.Attributes.Add("onkeydown", "EnterEvent(event, this);");

My page is still doing postback at Enter key. The idea is to stop the Enter postback if at least has 3 characters. Any idea or another approach?

我的页面仍在按 Enter 键进行回发。这个想法是在至少有 3 个字符时停止 Enter 回发。任何想法或其他方法?

> -----------------EDIT--------------------

> -----------------编辑--------------------

I added this function before the EnterEvent

我在 EnterEvent 之前添加了这个函数

    $(function () {
        $(':text').bind('keydown', function (e) { 
            if (e.keyCode == 13) 
                e.preventDefault();
        });
    });

But it blocks the Enter in all the page. Enter doesn't work AT ALL.

但它阻止了所有页面中的 Enter。输入根本不起作用。

> -----------------EDIT 2--------------------

> -----------------编辑 2--------------------

Well, I succeded! As I said in a previous comment, I tried like in the past adding a dummy button control and I transfered the event from the textbox and calling its click event. It's not CLEAN AT ALL but it worksand I'm in a hurry. Thank you all you guys for answering. If someone can help still I would appreciate it. I'll continue reviewing this question. Thanks.

嗯,我成功了!正如我在之前的评论中所说,我尝试像过去一样添加一个虚拟按钮控件,然后从文本框中传输事件并调用其单击事件。它一点也不干净,但它有效,我很着急。谢谢大家的回答。如果有人仍然可以提供帮助,我将不胜感激。我会继续回顾这个问题。谢谢。

    function EnterEvent(e, ctrl) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if (keycode == 13 && ctrl.value.length > 2) {
            $('[id$=Button1]').click();
        }
        else {
            return false;
        }
    }

<asp:TextBox ID="TextBox1" runat="server" onkeyup="EnterEvent(event, this)" />
<asp:Button ID="Button1" runat="server" OnClick="TextBox1_TextChanged" style="visibility:hidden;width:0;"/>

回答by Andy

Had a similar need to prevent an enter keypress (although, I didn't need to consider the input length condition as in this case) and found this which solved the problem... simpler than adding an entire script block if all you need to do is stop the form postback due to someone pressing enter in a text area.

有一个类似的需要来防止输入按键(虽然,我不需要像在这种情况下那样考虑输入长度条件)并发现这解决了问题......如果你需要的话,这比添加整个脚本块更简单由于有人在文本区域中按下 Enter 键,因此停止表单回发。

<asp:TextBox ID="tb_Input" runat="server" onkeypress="return event.keyCode != 13;"></asp:TextBox>

Thought I'd share this in case someone else is searching for this functionality only.

我想我会分享这个以防其他人只搜索这个功能。

回答by Remy

Try:

尝试:

e.preventDefault();

to stop the Enter event.

停止 Enter 事件。

I've built a little fiddler:

我已经建立了一个小提琴手:

http://jsfiddle.net/rblaettler/Y3b8F/5/

http://jsfiddle.net/rblaettler/Y3b8F/5/

Is this what you are trying to do? It only submits if you have more than 2 characters.

这是你想要做的吗?仅当您有超过 2 个字符时才提交。

回答by Flavia Obreja

<asp:TextBox ID="TextBox1" runat="server" onkeydown="return EnterEvent(event,this);" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" />

function EnterEvent(e, ctrl) {
            var keycode = (e.keyCode ? e.keyCode : e.which);
            if (keycode == 13 && ctrl.value.length > 2) {
                return false;
            }
            else {
                return true;
            }
        }