Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter in search box and redirect to another page

// catch enter code in search form in front page
$('#search').keypress(function (e) {
    var str = $('#search').val();
    var url = "default.aspx?search=" + str;
    if (e.keyCode == 13) {
        location.href = url;
    }
});

I don't know why this code doesn't work what I expected " When you enter something in input#search, check if it's not empty then redirect to another page ". I try to enter every line in console without checking event, it works!

How can I fix this and why it doesn't work ? Thanks for your consideration time :)

like image 990
Dzung Nguyen Avatar asked Dec 10 '25 19:12

Dzung Nguyen


2 Answers

You might try .keyup() instead of .keypress(). Keypress is not an official specification, and can have unfortunate consequences in some browsers.

like image 161
Jason T Featheringham Avatar answered Dec 13 '25 07:12

Jason T Featheringham


Put your domain including http for location href to work correctly

    // catch enter code in search form in front page
$('#search').keypress(function (e) {
    var str = $('#search').val();
    var domain = "http://www.yourdomain.com";
    var url = domain+"default.aspx?search=" + str;
    if (e.keyCode == 13) {
        location.href = url;
    }
});
like image 42
Dominic Green Avatar answered Dec 13 '25 08:12

Dominic Green