简体   繁体   English

jQuery自动完成-字符串数组

[英]jquery autocomplete - string array

My web form model(AdtFormModel) has this variable: 我的网络表单模型(AdtFormModel)具有以下变量:

public List<String> TemoinsVille { get; set; }

I use a list because I want the user to be able to dynamically add more 'TemoinsVille' in the form. 我使用列表是因为我希望用户能够在表单中动态添加更多的“ TemoinsVille”。 For now I have two textboxfor, when I fill both of them, my controller receives a string list with 2 items in it. 现在我有两个文本框,当我填满两个文本框时,我的控制器会收到一个包含2个项目的字符串列表。 I will add the feature to dynamically add TemoinsVille later. 我将添加功能以在以后动态添加TemoinsVille。

In my form, I want to use an auto-complete field for this TemoinVille variable. 在我的表单中,我想为该TemoinVille变量使用一个自动完成字段。 I use a method called 'AutocompleteTous' that works, I use it somewhere else in the form. 我使用一种称为“ AutocompleteTous”的方法,该方法有效,我在表单的其他地方使用它。 I don't know how to write the script so that the autocomplete works for a second TemoinVille input. 我不知道如何编写脚本,以便自动完成功能可用于第二个TemoinVille输入。

My JQuery code is: 我的JQuery代码是:

   var auto5 = $('#AdtFormModel_TemoinsVille').autocomplete({
        source: '@Url.Action("AutocompleteTous", "InvForm")',
        minLength: 3,
        delay: 400
    }).data("ui-autocomplete");
    auto5._renderItem = function (ul, item) {
        return $("<li>")
            .attr("ui-autocomplete-item", item)
            .append(item.label.split("|")[0])
            .appendTo(ul);
    };
    auto5._renderMenu = function (ul, items) {
        var that = this;
        $.each(items, function (index, item) {
            that._renderItemData(ul, item);
        });
        $(ul).addClass("dropdown-menu");
    };

In my view, I have t TemoinVille textboxfor inputs: 在我看来,我没有TemoinVille文本框用于输入:

@Html.TextBoxFor(x => x.AdtFormModel.TemoinsVille, new { Class = "form-control" }) @Html.ValidationMessageFor(x => x.AdtFormModel.TemoinsVille, null, new { @class = "text-danger" })

@Html.TextBoxFor(x => x.AdtFormModel.TemoinsVille, new { Class = "form-control" }) @Html.ValidationMessageFor(x => x.AdtFormModel.TemoinsVille, null, new { @class = "text-danger" })

the autocomplete works for the first input, but not for the second... 自动完成功能适用于第一个输入,但不适用于第二个输入...

Here is the html code for the two inputs: 这是两个输入的html代码:

<div class="invalidite-section">
        <input Class="form-control" id="AdtFormModel_TemoinsVille" name="AdtFormModel.TemoinsVille" type="text" value="" /> <span class="field-validation-valid text-danger" data-valmsg-for="AdtFormModel.TemoinsVille" data-valmsg-replace="true"></span>
    </div>

<div class="invalidite-section">
        <input Class="form-control" id="AdtFormModel_TemoinsVille" name="AdtFormModel.TemoinsVille" type="text" value="" /> <span class="field-validation-valid text-danger" data-valmsg-for="AdtFormModel.TemoinsVille" data-valmsg-replace="true"></span>
    </div>

Any suggestions? 有什么建议么?

You are targeting an id #AdtFormModel_TemoinsVille , that's the input right? 您要定位ID #AdtFormModel_TemoinsVille ,这是输入权吗? The ID must be unique on page. 该ID在页面上必须唯一。 So it will work only on first input it finds. 因此,它将仅在找到的第一个输入上起作用。

You need to target the elements by class, then the autocomplete should work in all inputs. 您需要按类别定位元素,然后自动完成应该在所有输入中起作用。

try something like this: 尝试这样的事情:

   var auto5 = $('.form-control input').autocomplete({ // I suppose .form-control is a div that contains the input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM