【问题标题】:How to use jquery autocomplete combobox with ajax如何使用带有 ajax 的 jquery 自动完成组合框
【发布时间】:2015-09-16 09:45:06
【问题描述】:

我对 jquery-ui 很陌生。我想将this 与ajax 一起使用。 (ASP.NET MVC 5) 我创建了 jquery-ui-combobox.js 并在 jquery(combobox) 函数中添加。

我想这样使用。这个论点(来源)正确吗?

    <script>
    $(function () {
        $("#combobox").combobox({source: function (request, response) {
                $.ajax({
                    url: "/Home/GetProjects",
                    dataType: "json",
                    data: {
                        term: request.term
                    },
                    type: "POST",
                    success: function (data) {
                        /*I dont know what I should here*/
                         })
                        );
                    }
                });});
        $("#toggle").click(function () {
            $("#combobox").toggle();
        });
    });
</script>

控制器:

        public JsonResult GetProjects(string term)
    {
        var list = new List<SelectListItem>();
        list.Add(new SelectListItem { Value = "1", Text = "ActionScript" });
        list.Add(new SelectListItem { Value = "2", Text = "AppleScript" });
        list.Add(new SelectListItem { Value = "3", Text = "Asp" });
        var res = list.Where(p => p.Text.Contains(term));
        return Json(res , JsonRequestBehavior.AllowGet);
    }


                                                                                                Please help.

【问题讨论】:

    标签: jquery ajax asp.net-mvc jquery-ui


    【解决方案1】:

    成功后你可以这样做...

     $("#combobox").append("<option value=''>Select</option>");
                        $.each(data, function (value, key) {
                            $("#combobox").append("<option value='"+key.Value+"'>"+key.Text +"</option>");
                        });
    

    【讨论】:

    • 好的。我补充说;谢谢@Raghava Nalkari。但是 $.Ajax 没有被触发。
    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多