3

我的网站主页上有一个功能,用户在其中输入菜名并按 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;
                }
            });
        }); 

您可以在http://khanawal.com/home.aspx看到该功能 任何帮助将不胜感激。

4

1 回答 1

1

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

$(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。

于 2013-08-08T09:12:22.997 回答