0

该操作返回一个 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>
4

1 回答 1

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) {
于 2013-05-20T21:06:30.760 回答