0

我正在尝试编写我的第一个 jQuery 自动完成示例。我需要能够从自动完成中选择多个值,所以我使用了此处提供的示例。

无论我做什么,当我输入“c”、“ch”等时,我都无法让文本框显示值“chicken”和“chickens”。我做错了什么?

我的控制器有这个动作方法

public JsonResult GetBirds()
{
    var result = new JsonResult
        {
            Data = new
                {
                    Birds = new List<string> {"chicken", "chickens"}
                },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };

    return result;
}

我的前端代码是这样的:

<script>
    $(function () {
        function log(message) {
            $("<div/>").text(message).prependTo("#log");
            $("#log").scrollTop(0);
        }

        $("#birds").autocomplete({
            source: "/Results/GetBirds",
            minLength: 1,
            select: function (event, ui) {
                log(ui ?
                "Selected: " + ui :
                "Nothing selected, input was " + this.value);
            }
        });
    });
</script>



<div class="demo">

    <div class="ui-widget">
        <label for="birds">Birds: </label>
        <input id="birds" />
    </div>

    <div class="ui-widget" style="margin-top:2em; font-family:Arial">
        Result:
        <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
    </div>

</div><!-- End demo -->
4

3 回答 3

0

使用 Rest Clint 或 Firebug Firefox 插件查看控制器返回的内容。

试试这个:

return Json(new {"chicken", "chickens"},JsonRequestBehavior.AllowGet);

于 2012-08-30T16:38:13.383 回答
0

您需要为每个对象构建一个包含“值”和“标签”的字典列表。

http://api.jqueryui.com/autocomplete/

于 2014-07-10T17:26:20.753 回答
0

您的来源格式不正确,忘记 json 结果中的“Birds”键。只需:

Data = new List<string> {"chicken", "chickens"}

或者

Data = new Dictionary<string, string> {
    {"chiken", "value1"},
    {"chikens", "value2"},
};
于 2014-07-29T18:16:02.617 回答