自动完成中不会显示任何结果

时间:2013-05-20 20:18:36

标签: jquery-ui jquery autocomplete

该操作返回JSonResult并且内容正确。我错过了自动填充代码中的内容了吗?

HTML:

<input type="text" class="span12" id="mytextbox" />

javascript:

<script>
    $(document).ready(function () {
        $("#mytextbox").autocomplete({
            minLength: 2,
            source: function(request,response) {
                $.ajax({
                    url: "MyController/MyAction",
                    type: "POST",
                    dataType: "json",
                    data: { term: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { firstname: item.FirstName, lastname: item.LastName, code: item.Code };
                        })) 
                    }
                })
            },
            messages: {
                noResults: "", results: ""
            }
        })
        .data("ui-autocomplete")._renderItem = function (ul, item) {
            return $("<li>")
              .append("<a>" + item.firstname + "<br>" + item.lastname + "</a>")
              .appendTo(ul);
        };
    })
  </script>

1 个答案:

答案 0 :(得分:0)

下一个代码可以使用,其中availableTags替换你的ajax。

<script>
    $(document).ready(function () {
      var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC"];

        $("#mytextbox").autocomplete({
            minLength: 2,
            source: availableTags,
            messages: {
                noResults: "", results: ""
            }
        }).data("ui-autocomplete").renderItem = function (ul, item) {
        return $("<li>")
          .append("<a>" + item.firstname + "<br>" + item.lastname + "</a>")
          .appendTo(ul);
    };
    });
</script>

这可能会有所帮助:http://api.jquery.com/jQuery.data/#jQuery-data-element

<强>更新

问题在于这一行:

.data("ui-autocomplete").renderItem = function (ul, item) {
你有

.data("ui-autocomplete").**_**renderItem = function (ul, item) {