在版本
2.1 of Twitter Bootstrap中,能够在Typeahead选项
was added中传递一个回调函数.但是,我一直困扰着使用jQuery ajax调用.
这是我到目前为止.
HTML
<form class="form-horizontal" action=""> <fieldset> <div class="control-group"> <label class="control-label" for="myTypeahead">User</label> <div class="controls"> <input type="text" id="myTypeahead" /> </div> </div> </fieldset> </form>
JavaScript(设置在jQuery $(document).ready函数中)
$("#myTypeahead").typeahead({ source: function (query,process) { $.ajax({ type: "POST",url: ServiceURL + "/FindUsers",data: "{ SearchText: '" + query + "' }",contentType: "application/json; charset=utf-8",dataType: "json",success: function (r1) { var users = []; if (r1.d.Records) { $(r1.d.Records).each(function (index,val) { users.push(val.User.Name); }); console.log(users); process(users); } } }); } });
当在Typeahead输入中键入test(或Test)时,不会显示Typeahead结果,但是从users变量记录的数据如下所示:
["Test User A","Test User B","Test User C","Test User D","Test User E","Test User F"]
为什么这不工作?
另外,作为参考,这里是Typeahead JavaScript for Bootstrap.