在搜索框中输入并重定向到另一个页面 [英] Enter in search box and redirect to another page

查看:88
本文介绍了在搜索框中输入并重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// 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.

这篇关于在搜索框中输入并重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆