简体   繁体   中英

catch the enter keyup event,while it shows the error 405:method not allowed

I'm writing a keyup event on a search input like:

$("#searchbox").keyup(function(e){
    var curKey = e.which; 
    var text = $(this).attr("value");
    if(curKey == 13){
        $(".menu_tab").removeClass("menu_bg");
        $(".mainGreyBox").css("display","none");
        $("#aboutSearchResult").css("display","block");

    }else{
        var text = $(this).attr("value");
        console.log(text);
        $("#searchresult").empty();
        var SearchMan = new SearchModel().init(text); //显示search列表
        $(".global_acResults").show();
    }

I want to show a new page which id is "aboutSearchResult" when I enter the search words and then press the enter on the keyboard.But I find that the page I want to show flash past and then the page shows like: 在此处输入图片说明

But I don't know why,looking for your help,thanks.

ps: #searchbox is an input element.

The keydown event generated when you press the Enter key in the text box is not being handled, and is causing the form that the input element is a member of to be submitted (to a URI that isn't expecting a form to be submitted to it, hence the error).

Bind your event handler to keydown , not keyup , and include

event.preventDefault();

in the branch for Enter ( curKey == 13 ) to prevent it from triggering the default behavior (of submitting the form).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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