jQuery自动完成 - 传递选定的组合框值

时间:2016-08-09 08:27:53

标签: jquery jquery-ui combobox autocomplete

我需要将“竞争对手”组合框的选定值传递给附加到“外部参照”文本框的自动完成。自动填充仅传递第一项的值,即使选择了最后一项也是如此。如何解决?

控制:

@Html.DropDownList("Competitor", (IEnumerable<SelectListItem>)ViewBag.Competitors, htmlAttributes: new { @class = "form-control" })
@Html.TextBox("xref", null, new { @class = "control-label col-lg-4 col-md-4 col-xs-4" })

自动填充脚本:

    $('#xref').autocomplete({
        source: '/Products/TagSearch?Competitor=' + $('#Competitor').val()
    });

控制器:

    public ActionResult TagSearch(string term, int Competitor)
    {
        string[] tags = { };
        var myList = new List<string>();
        var xrefs = db.CrossReferences.Where(p => p.CompetitorId == Competitor).Where(p => p.Reference.StartsWith(term));
        if (xrefs != null)
        {
            foreach (var item in xrefs)
            {
                 myList.Add(item.Reference.ToString());
            }
            tags = myList.ToArray();
        }
        return this.Json(tags.Where(t => t.StartsWith(term)),
                        JsonRequestBehavior.AllowGet);
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

找到解决方案。它足以将其包装在keyup函数中:)

$("#xref").keyup(function () {
    $('#xref').autocomplete({
        source: '/Products/TagSearch?Competitor=' + $('#Competitor').val()
    });
});