jquery - 为什么新添加的输入没有调用自动完成功能?

标签 jquery html

我现在已经正确地在 View 中的一个输入上实现了自动完成功能我想允许用户通过(在 js 中添加按钮)添加更多输入问题是新添加的输入没有调用自动完成功能为什么?

自动完成功能

$(document).ready(function () {
            $(".AOI").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "/Test/SearchAOI",
                        type: "POST",
                        dataType: "json",
                        data: { Prefix: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.AOIName, value: item.AOIName };
                            }))

                        }
                    })
                },
                messages: {
                    noResults: '',
                    results: function (resultsCount) { }
                }
            });
        })

使用户能够添加更多输入的功能

var areaIndex = "b";
        $("#addAOI").click(function () {
            //create the elements representing the new object, giving them a fake indexer
            areaIndex = areaIndex + "b";
            $("#AOIFields").append("<td><input type='hidden' name='AOI.Index' value='"+areaIndex+"' style='display:none;' /> <input type='text' class='form-control AOI' name='AOI["+areaIndex+"]' /></td>");
        })

部分 View

         <input type="hidden" name="AOI.Index" value="123"style="display:none"/>

                        <input type="text" class="form-control AOI" name="AOI[123]" />
                    </td>
                </tr>

最佳答案

你需要将自动完成绑定(bind)到新的添加元素,这样你就可以做

$("#addAOI").click(function () {
    //create the elements representing the new object, giving them a fake indexer
    areaIndex = areaIndex + "b";
    var $container = $("#AOIFields");
    $container.append("<td><input type='hidden' name='AOI.Index' value='"+areaIndex+"' style='display:none;' /> <input type='text' class='form-control AOI' name='AOI["+areaIndex+"]' /></td>");
    $container.find(".AOI").last().autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Test/SearchAOI",
                type: "POST",
                dataType: "json",
                data: { Prefix: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.AOIName, value: item.AOIName };
                    }))

                }
            })
        },
        messages: {
            noResults: '',
            results: function (resultsCount) { }
        }
    });
})

为了获得更好的结果,您可以将自动完成选项存储在 var 中

关于jquery - 为什么新添加的输入没有调用自动完成功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873327/

相关文章:

javascript - 表单未提交 laravel

javascript - 在 chrome 扩展中调用通知函数时打开不同的页面定义?

html - 使用 :cover; adds horizontal scroll 的完整背景

Javascript:将对象值数组放入div

javascript - 如何遍历 document.body

html - CSS 选择器反转 :focus + label logic

html - 使用 <div> 作为父元素

javascript - JQuery 可排序迭代元素

jquery - 使用带有文本导航的 jQuery FlexSlider 插件?

jquery - 如何使用 css 对自定义目录进行编号?