0
        $(function(){

        var orthoObjs = {};
        var orthoNames = [];


        var throttledRequest = _.debounce(function(query, process){

            $.ajax({
                url: 'json/ortho4.json'
                ,cache: false
                ,success: function(data){


                orthoObjs = {};
                orthoNames = [];


                _.each( data, function(item, ix, list){



                orthoNames.push( item.searchPhr );


                        orthoObjs[ item.searchPhr ] = item;

                    });


                    process( orthoNames );
                }
            });
        }, 300);


        $(".typeahead").typeahead({
            source: function ( query, process ) {


                throttledRequest( query, process );

            }

        ,updater: function (item) {

        var url = "orthoObjs[item.searchUrl]";

        window.location = url;

使重定向起作用的最佳方法是什么?我见过类似的问题,但无法使其正常工作。关于 typeahead 的文档不是很好。我为每个函数使用 underscore.js。只需要一个在用户选择时重定向的简单搜索查询。

4

1 回答 1

1

我实际上得到了这个工作。我得到了一点帮助...但是就在这里。有JSON文件..

[
   { "id":1,  "searchUrl":"invisalign.html", "name":"invisalign" }
    ,{ "id":2, "searchUrl":"invisalign.html", "name":"invisalign teen"  }
    ,{ "id":3, "searchUrl":"clearbraces.html", "name":"clear braces"  }
]

和 HTML 代码....

这里有很多好东西.. http://fusiongrokker.com/post/heavily-customizing-a-bootstrap-typeahead

和搜索代码..

<form method="post" id="myForm" class="navbar-search pull-left">  
      <input 
            type="text"
            class="search-query typeahead"
            placeholder="Search Our Website"
            autocomplete="off"
            data-provide="typeahead"
            />
            <i class="fa-icon-search icon-black"></i>  
</form> </li>

 $(function(){

      var bondObjs = {};
      var bondNames = [];

      $(".typeahead").typeahead({
        source: function ( query, process ) {

          //get the data to populate the typeahead (plus an id value)
          $.ajax({
            url: '/json/bonds.json'
            ,cache: false
            ,success: function(data){

              bondObjs = {};
              bondNames = [];


              _.each( data, function(item, ix, list){


                bondNames.push( item.name );


                bondObjs[ item.name ] = item.searchUrl;
              });


              process( bondNames );
            }
          });
        }
        , updater: function ( selectedName ) {

          window.location.href =bondObjs[ selectedName ];

        }
      });
    });
  </script>
于 2013-07-12T17:44:15.733 回答