jQuery自动完成触发器返回select

时间:2016-05-12 13:11:57

标签: javascript jquery asp.net autocomplete

我搜索了很多地方,但我不明白jquery足以成功地使用我找到的答案。

我需要doubletab返回以触发我的文本框 ontextchanged 事件,以便我的gridview正在更新。

我的自动完成脚本正常运行,看起来像这样:

<script type="text/javascript" lang="ja">
    $(function () {
        $('#tbCompany').autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "Autocomplete.asmx/GetCompanyNames",
                    data: "{ 'searchTerm': '" + request.term + "' }",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    success: function (result) {                          
                        response(result.d);                            
                    },                       
                    error: function (result) {
                        alert('There is a problem processing your request');
                    }
                });
            },
            minLength: 0
        });
    });
</script>


<asp:TextBox runat="server" ID="tbCompany" placeholder="Bitte Ausfüllen" ClientIDMode="static" OnTextChanged="tbCompany_TextChanged" Width="400px" Visible="false" TabIndex="1" />


 <asp:UpdatePanel runat="server">
      <ContentTemplate>
           <asp:GridView ID="gvVerzeichniss" runat="server" AutoGenerateColumns="false" OnRowCommand="gvVerzeichniss_RowCommand" CssClass="mGrid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr" GridLines="None" ShowHeader="false" style="width: 1300px !important;">
                <Columns>
                        <asp:ButtonField ButtonType="Link" Text="view" CommandName="view" HeaderText="Show" Visible="true" ControlStyle-Width="40px" ControlStyle-ForeColor="#428bca"/>
                        <asp:CheckBoxField HeaderText="Active" ReadOnly="false" DataField="Active" ControlStyle-Width="10px" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="tbCompany"/>
        </Triggers>
  </asp:UpdatePanel>

它应该在选择项目时触发文本框的 ontextchanged 事件,但我不知道该怎么写。

1 个答案:

答案 0 :(得分:0)

基本上你是在自动完成内发送ajax调用,而自动完成绑定是在从服务收到数据之后。

您的result.d必须有json个数据。

如果我理解你正在寻找类似的东西

<script type="text/javascript" lang="ja">
    $(function () { 
          $.ajax({
                url: "Autocomplete.asmx/GetCompanyNames",
                data: "{ 'searchTerm': '" + request.term + "' }",
                type: "POST",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (result) {                          
                  $('#'+'<%= tbCompany.ClientID %>').autocomplete({
                    source: result.d,
                    minLength: 0
                  });                        
                },                       
                error: function (result) {
                    alert('There is a problem processing your request');
                }
            });
    });
</script>