Skip to main content
 首页 » 编程设计

jquery之将 Typeahead.js 与 ASP.Net Webmethod 集成

2026年10月07日3telwanggs

我正在寻找将 Typeahead.js 与 Asp.Net Webmethod 结合使用的示例。

我有来自 http://twitter.github.io/typeahead.js/examples/ 的示例使用基本示例,但我不完全理解如何使用 asp.net webmethod 实现 Bloodhound 功能。

TypeAhead 函数正在调用 WebMethod(我可以在调试器中看到该步骤),但 TypeAhead 列表中没有返回任何内容。

这是标记:

<div class="input-group"> 
    <asp:TextBox ID="tboxText" runat="server" CssClass="form-control autocomplete" placeholder="Look Up"></asp:TextBox> 
    <span class="input-group-btn"> 
        <asp:Button ID="btnAddItem" runat="server" Text="Add" CssClass="btn btn btn-amethyst" /> 
    </span> 
</div> 

这是 jquery:

$(document).ready(function () { 
        var textlookup = new Bloodhound({ 
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 
            queryTokenizer: Bloodhound.tokenizers.whitespace, 
            remote: '/WebServices/InternalMethods.asmx/TextAutocomplete?param=%QUERY'  
        }); 
 
        textlookup.initialize(); 
 
        $('.autocomplete').typeahead({ 
            hint: true, 
            highlight: true, 
            minLength: 1 
        }, 
        { 
            name: 'textlookup', 
            displayKey: 'value', 
            source: textlookup.ttAdapter() 
        }); 
    }); 

这是 WebMethod:

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public List<string> TextAutocomplete(string param) 
    { 
        return Suggestions.TextAutocomplete(param); 
    } 

感谢任何帮助。

请您参考如下方法:

看起来您可能需要在 C# 中将参数作为查询字符串获取。尝试使用 return Suggestions.TextAutocomplete(Request.QueryString["param"]);