Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery克隆的django可选自动完成下拉列表不工作_Javascript_Jquery_Django_Autocomplete - Fatal编程技术网

Javascript jQuery克隆的django可选自动完成下拉列表不工作

Javascript jQuery克隆的django可选自动完成下拉列表不工作,javascript,jquery,django,autocomplete,Javascript,Jquery,Django,Autocomplete,正如标题所述,我的页面上有一个django可选的自动完成表单字段,我正在尝试在页面加载后动态克隆该字段。克隆部分可以工作,但自动完成字段不起作用。我不知道django Selective的内部结构,而且我还在学习jQuery,所以我还不知道如何“修复”autoselect功能 我的一般方法是:在div容器中使用django模板变量呈现表单小部件,然后克隆div容器 HTML 根据SunnySydeUp的评论,我做了以下修改: jQuery // This works, just the clo

正如标题所述,我的页面上有一个django可选的自动完成表单字段,我正在尝试在页面加载后动态克隆该字段。克隆部分可以工作,但自动完成字段不起作用。我不知道django Selective的内部结构,而且我还在学习jQuery,所以我还不知道如何“修复”autoselect功能

我的一般方法是:在div容器中使用django模板变量呈现表单小部件,然后克隆div容器

HTML

根据SunnySydeUp的评论,我做了以下修改:

jQuery

// This works, just the cloned form lacks "autocomplete" functionality.
var autocompleteArray = theDiv.find('.autocomplete');
var acClone = autocompleteArray.clone();
table.find(".column").append(acClone);
// Clone the div and all its contents
var acClone = autocompleteArray.clone(true, true);
// Get the children of the div
var acCloneChildrenArray = acClone.children();
// iterate through the children array, modify ids where they exist to make them unique
// e.g., unique id contained in idSuffix.
for (var i = 0; i < acCloneChildrenArray.length; i++) {
    // if it has an id, make it unique
    if (acCloneChildrenArray[i].getAttribute('id')) {
        var ident = acCloneChildrenArray[i].getAttribute('id')
        acCloneChildrenArray[i].setAttribute('id', ident+'_'+idSuffix);
    };
};
// Clone the div
// We leave clone deepcopy flags at default==false
var acClone = autocompleteArray.clone();

// Get the children of the div
// You don't need to set unique id's on the children, it's irrelevant.

// Bind the new selectable fields to the event handlers.
window.bindSelectables('body');

我以前没有使用过
django-selectable
,但我猜它使用javascript/jQuery来实现自动完成。当您调用
clone
时,它不会为下拉列表克隆javascript。试着做:

var acClone = autocompleteArray.clone(true);
传入true将使jQuery也复制该输入上的数据和事件


谢谢您的光临。我已经根据您的反馈对代码进行了修改。您所处的轨道是正确的,但现在需要将事件绑定到克隆的下拉列表。我不知道怎么做。在快速浏览django selectable的javascript源代码(jquery.dj.selectable.js)之后,它似乎在做
window.bindSelectables('body')。也许试一下?是的!这只需稍加修改:您必须将.clone()的deepcopy标志设置为false。有一个小错误(每个表单都有一个重复的下拉按钮,但是自动完成和下拉功能在那里。非常感谢。我尝试了大约20种不同的方法,总的来说都是为了让它工作。
var acClone = autocompleteArray.clone(true);