最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Don't submit form when I press enter key in a textbox - Stack Overflow

programmeradmin1浏览0评论

i have a text-box in my form. I write a JavaScript function to add some functionality when user enter some value in text box and then press Enter button. When I press Enter button, first , it perform my JavaScript functionality but then it shows the validations message(it submits the form). How to solve this problem

Here is my texbox

<asp:TextBox ID="txt_CopyFrom" runat="server" onkeypress="searchKeyPress(event);" ClientIDMode="Static"></asp:TextBox>

And here is my JavaScript function

function searchKeyPress(e) {
        if (e.keyCode === 13) {
                .
                .
                .

        }
        return false;
}

i have a text-box in my form. I write a JavaScript function to add some functionality when user enter some value in text box and then press Enter button. When I press Enter button, first , it perform my JavaScript functionality but then it shows the validations message(it submits the form). How to solve this problem

Here is my texbox

<asp:TextBox ID="txt_CopyFrom" runat="server" onkeypress="searchKeyPress(event);" ClientIDMode="Static"></asp:TextBox>

And here is my JavaScript function

function searchKeyPress(e) {
        if (e.keyCode === 13) {
                .
                .
                .

        }
        return false;
}
Share Improve this question edited Sep 30, 2013 at 20:45 Ahmad Abbasi asked Sep 30, 2013 at 20:36 Ahmad AbbasiAhmad Abbasi 1,7766 gold badges29 silver badges43 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Prevent the default action by using preventDefault()

function searchKeyPress(e) {
    e.preventDefault();
    if (e.keyCode === 13) {
            .
            .
            .

    }
    return false;
}
发布评论

评论列表(0)

  1. 暂无评论