Reputation: 9244
// 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
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
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