jQuery autocomplete with many inputs - django-dynamic-formset

浪尽此生 提交于 2019-12-23 03:12:43

问题


I'm using jQuery autocomplete feature (http://jqueryui.com/autocomplete) and Django dynamic-formset (http://code.google.com/p/django-dynamic-formset/). One of my formset fields needs to use autocomplete, so I attach it using (simplified):

$('input[name$=select]').autocomplete({source:'my_url/', minLength: 3});

I call this on document ready and it works fine (gets data from ajax). However, if I add new row with django-dynamic-formset and handle its 'added' event with:

function(row){
    $(row).find('input[name$=select]').autocomplete({source:'my_url/', minLength: 3});
}

it doesn't work with newly added rows. What's wrong?

UPDATE: Tried to use classes instead - no result Tried to use on()/live() - no result Tried to destroy autocomplete after new row is added - no result (it destroys, but does no create).


回答1:


I just tested this code with jquery.formset-1.2 and it definitely works:

function enableAutocomplete(context) {
    $('input[name$=select]', context || null).autocomplete({source:'my_url/', minLength: 3});
}

$(document).ready(function() {
    $('.foo').formset({
        added: function(row) {
            enableAutocomplete(row);
        }
    });

    enableAutocomplete();
});


来源:https://stackoverflow.com/questions/14686456/jquery-autocomplete-with-many-inputs-django-dynamic-formset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!