jQuery自动完成动态创建的输入 [英] jQuery autocomplete for dynamically created inputs

查看:27
本文介绍了jQuery自动完成动态创建的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用带有动态创建输入(再次使用 jQuery 创建)的 jQuery 自动完成时遇到问题.我无法自动完成以绑定到新输入.

I'm having an issue using jQuery autocomplete with dynamically created inputs (again created with jQuery). I can't get autocomplete to bind to the new inputs.

自动完成

      $("#description").autocomplete({
         source: function(request, response) {
            $.ajax({
                url: "../../works_search",
                dataType: "json",
                type: "post",
                data: {
                    maxRows: 15,
                    term: request.term
                },
                success: function(data) {
                    response($.map(data.works, function(item) {
                        return {
                                label: item.description,
                                value: item.description
                        }
                    }))
                }
            })
        },
        minLength: 2,
    });


带有输入的新表格行

var i = 1;
var $table = $("#works");
var $tableBody = $("tbody",$table);

$('a#add').click(function() {
    var newtr = $('<tr class="jobs"><td><input type="text" name="item[' + i + '][quantity]" /></td><td><input type="text" id="description" name="item[' + i + '][works_description]" /></td></tr>');
    $tableBody.append(newtr); 
    i++;
});

我知道问题是由于页面加载后创建的内容,但我不知道如何解决它.我已经阅读了几个相关的问题并遇到了 jQuery live 方法,但我仍然陷入困境!

I'm aware that the problem is due to the content being created after the page has been loaded but I can't figure out how to get around it. I've read several related questions and come across the jQuery live method but I'm still in a jam!

有什么建议吗?

推荐答案

首先,您需要存储 .autocomplete() 的选项,例如:

First you'll want to store the options for .autocomplete() like :

var autocomp_opt={
         source: function(request, response) {
            $.ajax({
                url: "../../works_search",
                dataType: "json",
                type: "post",
                data: {
                    maxRows: 15,
                    term: request.term
                },
                success: function(data) {
                    response($.map(data.works, function(item) {
                        return {
                                label: item.description,
                                value: item.description
                        }
                    }))
                }
            })
        },
        minLength: 2,
    };

使用 class 属性来标记 input 更简洁,例如:

It's more neat to use the class attribute for marking the input, like:

<input type="text" class="description" name="item[' + i + '][works_description]" />

最后,当你创建一个新的表格行时,应用 .autocomplete() 和已经存储在 autocomp_opt 中的选项:

Last, when you create a new table row apply the .autocomplete() with the options already stored in autocomp_opt:

$('a#add').click(function() {
    var newtr = $('<tr class="jobs"><td><input type="text" name="item[' + i + '][quantity]" /></td><td><input type="text" class="description" name="item[' + i + '][works_description]" /></td></tr>');
    $('.description', newtr).autocomplete(autocomp_opt);
    $tableBody.append(newtr); 
    i++;
});

这篇关于jQuery自动完成动态创建的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆