在搜索框中输入并重定向到另一个页面

问题描述:

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

我不知道为什么这段代码不符合我的预期当你输入的东西时输入#search,检查它是否为空,然后重定向到另一页。我尝试在没有检查事件的情况下进入控制台中的每一行,它有效!

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

您可以尝试.keyup()而不是.keypress()。 Keypress不是官方规范,在某些浏览器中会产生不幸的后果。

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