1

有没有办法将一个项目附加到自动完成下拉列表的末尾,该下拉列表将包含 1-5 个项目?我使用过open,但是因为我在加载时在下拉列表中附加了一个微调器,所以它不能很好地工作。

jQuery:

$("#search").autocomplete({
  source: function(request, response) {
    response([{loading: true}]);
    return $.ajax({
      url: "/search",
      timeout: 10000,
      dataType: "json",
      data: {
        query: request.term
      },
      success: function(data) {
        var results;
        results = [];
        results = $.map(data, function(result) {
          return {
            label: result.name,
          };
        });
        return response(results);
      }
    });
  }
  //open doesn't work because I have a spinning effect that opens a dropdown
  //when the search is loading.
  //open: function() {
  //  alert('test');
  //}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
  if(item.loading) {
    return $( "<li id='spinner'></li>" )
      .appendTo( ul );
  }
};

谢谢!

4

1 回答 1

4

push在将其发送到response回调之前,您应该能够将任何您想要的项目放到结果列表中:

$("#search").autocomplete({
    source: function(request, response) {
        response([{loading: true}]);
        $.ajax({
            url: "/search",
            timeout: 10000,
            dataType: "json",
            data: {
                query: request.term
            },
            success: function(data) {
                var results = $.map(data, function(result) {
                    return {
                        label: result.name,
                    };
                });

                results.push({ label: "My Custom Item" });
                response(results);
            }
        });
    }
});

示例:http: //jsfiddle.net/m3MGH/1/

于 2012-08-23T00:37:40.720 回答