Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery捕获enter事件并重定向_Jquery_Redirect_Enter - Fatal编程技术网

使用jquery捕获enter事件并重定向

使用jquery捕获enter事件并重定向,jquery,redirect,enter,Jquery,Redirect,Enter,我在我的网站主页上有一个功能,用户在其中输入菜名并按enter键,应该捕获enter事件,它应该重定向到listings页面以及菜名作为querystring 我正在使用下面的代码,当我单击enter时,它只在第二次工作 $(document).ready(function () { $(document).keypress(function (e) { if (e.which == 13) { /

我在我的网站主页上有一个功能,用户在其中输入菜名并按enter键,应该捕获enter事件,它应该重定向到listings页面以及菜名作为querystring

我正在使用下面的代码,当我单击enter时,它只在第二次工作

 $(document).ready(function () {
            $(document).keypress(function (e) {
                if (e.which == 13) {
                    // enter pressed
                    var searchKeyWord = $("#SearchTextBox").val();
                    window.location.href = "/Listings.aspx?K=" + searchKeyWord;
                }
            });
        }); 
您可以在以下位置看到功能:
如果您有任何帮助,我们将不胜感激。

只要未选择搜索框,它似乎就可以工作。尝试绑定到两者

$(document).ready(function () {
        function handleEnter(e) {
            if (e.which == 13) {
                // enter pressed
                var searchKeyWord = $("#SearchTextBox").val();
                window.location.href = "/Listings.aspx?K=" + searchKeyWord;
            }
        }

        $(document).keypress(handleEnter);
        $("#SearchTextBox").keypress(handleEnter);

}); 

如果这不能解决问题,那么代码中的其他地方可能有问题。在代码中搜索stopPropagation

尝试添加
e.preventDefault()在ifI之前在if之后添加它…并且它起作用了…谢谢@petehaha,我只是在编辑这个之后说,首先我尝试在之前添加它,如果…我不能键入任何内容…哈哈…)。。你喜欢我的网站吗?这是一个新的开始…非常感谢你帮助我…有一个美好的生活。是的,非常好。它让我饿了!