javascript - 如何使用 jQuery Autocomplete item click 重定向到另一个页面

标签 javascript jquery asp.net css ajax

当我点击 Autocomplate Text Box 时,我需要重定向到另一个页面。我试着把

select: function(event, ui) {
                            window.location.href = 'Search.aspx?q=' + ui.item.value;;
                        },

但点击后没有反应?我需要什么来解决这个问题?

    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>


 <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "AutoCompleteService.asmx/GetAutoCompleteData",
                        data: "{'TurAdi':'" + document.getElementById('txtSearch').value + "'}",
                        idm: document.getElementById('txtSearch').value,
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        select: function(event, ui) {
                            window.location.href = 'Search.aspx?q=' + ui.item.value;;
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }

    </script>


 <asp:TextBox ID="txtSearch" runat="server" CssClass="searchtxtb oval autosuggest" Width="172px"  Height="25px"  BackColor="White"  Font-Bold="True" ForeColor="#666666" onfocus="if(this.value =='ARA:Tur, bölge veya belde adı' ) this.value=''"
                           onblur="if(this.value=='') this.value='ARA:Tur, bölge veya belde adı'" value="ARA:Tur, bölge veya belde adı"></asp:TextBox> 

最佳答案

select: function (event, ui) { ... } 应该移动到 source 的同级,因为它是 的一个选项自动完成不是ajax

像这样-

 <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest").autocomplete({
                // Put it here, as part of the autocomplete options.
                select: function(event, ui) {
                    window.location.href = 'Search.aspx?q=' + ui.item.value;;
                },
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "AutoCompleteService.asmx/GetAutoCompleteData",
                        data: "{'TurAdi':'" + document.getElementById('txtSearch').value + "'}",
                        idm: document.getElementById('txtSearch').value,
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },

                        // Moved out of here, since it was an ajax
                        // option and it is unrelated to ajax.

                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }

    </script>


 <asp:TextBox ID="txtSearch" runat="server" CssClass="searchtxtb oval autosuggest" Width="172px"  Height="25px"  BackColor="White"  Font-Bold="True" ForeColor="#666666" onfocus="if(this.value =='ARA:Tur, bölge veya belde adı' ) this.value=''"
                           onblur="if(this.value=='') this.value='ARA:Tur, bölge veya belde adı'" value="ARA:Tur, bölge veya belde adı"></asp:TextBox> 

关于javascript - 如何使用 jQuery Autocomplete item click 重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23461506/

相关文章:

javascript - 页面的水平滚动在 div jquery 中不起作用

c# - 避免删除 Azure 云服务发布上的文件

asp.net - HttpRequestValidationException 和跨站脚本 XSS

c# - IE 中的 ASP.NET 闪烁

javascript - 如何设置 Twitter 关注按钮,在关注后启用另一个按钮

javascript - 在基于 HTML5 的游戏中使用 Javascript Cookie 跟踪 HP

javascript - Trello API : list all cards for a given date range considering last activity date of the card in Trello?

javascript - 如何向 React 脚本生成的脚本添加 async 属性(在 create-react-app 模板上的 npm run build 阶段)

javascript - 删除 DOM 中的元素

javascript - 使用 ajax 在 laravel 上的 VerifyCsrfToken.php 第 67 行中的 TokenMismatchException