在我的MVC应用程序中,我使用了自动完成功能来填写一些输入框。
在我的一种观点中,这很好。
但是在模式弹出窗口中,它不起作用。进行了aajax调用,以从数据库中获取列表,但该列表未出现在搜索框下方。

从这里调用弹出窗口;

<a class="btn btn-info btn-xxs get-tender"
   href="#edit-tender-form"
   data-toggle="modal"
   data-tac-tender-url="/Tender/Get"
   data-tac-tender-status="2,Unsuccessful"
   data-tac-tender-id="5">Edit</a>


在“编辑投标”表单中,搜索输入框

<span class="ui-helper-hidden-accessible" role="status" aria-live="polite">10 results are available, use up and down arrow keys to navigate.</span>
<input style="width: 300px;" id="searchTerm" class="input-validation-error" name="searchTerm" type="search" data-tac-autocomplete="/Company/AutocompleteCompany" autocomplete="off">


以下javascript为包含data-tac-autocomplete属性的所有输入搜索框连接了自动完成功能;

var createAutocomplete = function () {
    var $input = $(this);
    var options = {
        source: $input.attr("data-tac-autocomplete"),
        select: updateAutocompleteForm,
        close: errorAutocompleteForm
    };
    $(".errorNotSelected").hide();
    $input.autocomplete(options);
};

$("input[data-tac-autocomplete]").each(createAutocomplete);


服务器代码上的断点表明它可以按预期工作;

[Authorize]
public ActionResult AutocompleteCompany(string term)
{
    var companyTypeId = this.GetCompanyTypeId();

    var model =
        this.TacUoW.GetCompanyAutocomplete(term, companyTypeId).Take(10).Select(
            x => new
            {
                label = string.Format("{0} - {1}", x.Company, x.Trade),
                id = x.CompanyId
            });
    return this.Json(model, JsonRequestBehavior.AllowGet);
}


那么,为什么在自动填充输入框下看不到自动填充的公司列表?

最佳答案

它可能落后于模态。

var createAutocomplete = function () {
var $input = $(this);
var options = {
    source: $input.attr("data-tac-autocomplete"),
    select: updateAutocompleteForm,
    close: errorAutocompleteForm,
    appendTo: $("#edit-tender-form")
};
$(".errorNotSelected").hide();
$input.autocomplete(options);
};


appendTo会将其附加到表单,如果不起作用,请尝试将其添加到模式元素

10-06 05:29
查看更多