1

I have an issue where I'm trying to execute a search query from a textbox and attempting to use enter to initiate the search; however, it doesn't seem to be working. I'm wondering if vb.net/asp.net is doing a postback or something that I'm not aware of.

ASPX Page

 <form class="navbar-search">
     <input id="searchbox" type="text" class="search-query span3" placeholder="Search" />
     <div class="icon-search"></div>
 </form>

JQuery

$('#searchbox').keyup(function (e) {
    //alert("this will execute");
    if (e.which == 13) {
        alert("this will not execute");
        //window.location.href = "http://www.google.com/";
    }
});

I can't get the alert box to execute or the change location to work... if anyone can help I appreciate it.

daveomcd
  • 6,367
  • 14
  • 83
  • 137
  • Works here: http://jsfiddle.net/YAfeN/ -- Is your input actually an ASP.NET control? – tymeJV May 01 '13 at 13:57
  • 1
    It works only if you press enter key because of the condition e.which ==13 http://jsfiddle.net/M2GPb/ – cheedep May 01 '13 at 13:58
  • 1
    you can't add a form into ASP.Net. ASP.Net add's a form be default for the __doPostback – Liam May 01 '13 at 14:00
  • Yes but when I press the enter key in my asp page the page refreshes instead of executing the code. I'm unsure why this is happening. – daveomcd May 01 '13 at 14:00
  • @Liam what do you mean you can't add a form into ASP.NET? His form doesn't have `runat=server` on it, and as long as it isn't nested in another form, he is OK. Although chances are he has put this form inside another one... – MikeSmithDev May 01 '13 at 14:07
  • @MikeSmithDev You cannot have a form in a form in HTML. When you create a ASP.Net page it add's a form by default, you never see it unless you look at the HTML. So you can never (in ASP.Net) add another form into the page. It will break the postback process. This is regardless of runat status – Liam May 01 '13 at 14:34
  • @Liam That is what I said, that you cannot nest forms. ASP.NET doesn't "automatically" add a form to the page... but their typical webforms masterpage template *does* have a form directly after the body tag, but there is *no* requirement to do it like that (such that all content is inside a form). I have multiple forms on my ASP.NET pages with no problem... and none are nested. The only limitation is that you can only have *one* form with `runat=server`. – MikeSmithDev May 01 '13 at 15:15

2 Answers2

1

Try this:

$("#searchbox").keypress(function (e) {
    //alert("this will execute");
    var code = e.keyCode || e.which;
    if (code == 13) {
        alert("this will not execute");
        //window.location.href = "http://www.google.com/";
    }
});

and to stop page from refreshing on-enter use this:

$(document).keydown(function (e) {
    var code = e.keyCode || e.which;
    if (code === 13) {
        e.preventDefault();
    }
});

FIDDLE

palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

Please try this.

$("#searchbox").live("keyup", function(event) {
        if (event.keyCode == 13) {
            //window.location.href = "http://www.google.com/";
        }
    });

This was worked for me...

mmpatel009
  • 921
  • 4
  • 11
  • 25