简体   繁体   English

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

[英]Prevent postback on Enter in a asp.net inputfield

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. 这就是我创建这个javascript函数的原因。 Because it avoids the Enter postback at any character input 因为它避免了在任何字符输入处输入回发

    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. 我的想法是,如果至少有3个字符,则停止输入回发。 Any idea or another approach? 任何想法或其他方法?

> -----------------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。 Enter doesn't work AT ALL. 输入不起作用。

> -----------------EDIT 2-------------------- > ----------------- EDIT 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. 正如我在之前的评论中所说的那样,我在过去尝试添加一个虚拟按钮控件,我从文本框转移了事件并调用了它的click事件。 It's not CLEAN AT ALL but it works and 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;"/>

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. 我以为我会分享这个以防其他人只是在搜索这个功能。

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个,则仅提交。

<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;
            }
        }

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

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