如何使用jQuery自动完成项目单击重定向到另一个页面

时间:2014-05-04 20:59:36

标签: javascript jquery asp.net css ajax

当我点击自动填充文本框时,我需要重定向到另一个页面。我试着把

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> 

1 个答案:

答案 0 :(得分:1)

select: function (event, ui) { ... }应移至source的兄弟,因为autocomplete不是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>