Tzu ng
Tzu ng

Reputation: 9244

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 :)

Upvotes: 3

Views: 7843

Answers (2)

Dominic Green
Dominic Green

Reputation: 10258

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

Upvotes: 1

Jason
Jason

Reputation: 3485

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

Upvotes: 2

Related Questions