jQuery, автоматический завершенный для динамично сгенерированных текстовых полей

Как насчет того, чтобы запуститься в источнике?

http://msdn.microsoft.com/sv-se/fsharp/cc742182 (en-us) .aspx

14
задан 6 October 2009 в 12:41
поделиться

4 ответа

Основная проблема, я думаю, заключается в том, что вы вызываете автозаполнение вне обработчика кликов. Таким образом, автозаполнение настраивается при загрузке страницы, а не при нажатии кнопки.

Чтобы решить эту проблему, измените код следующим образом:

$(function() {

    $("#button_newproduct").click(function() {

        var newItem = $("<tr><td><input type='text'name='td_products["+counter+"]'/></td></tr>");

        $("#products_table > tbody").append(newItem); 

        var data = "Core celectors cttributes craversing canipulation CSS cvents cffects cjax ctilities".split(" "); 
        newItem.find('input').autocomplete(data);

    });
});

Теперь автозаполнение устанавливается для каждого нового элемента, а не один раз в начале.

11
ответ дан 1 December 2019 в 13:33
поделиться

You need to add the autocomplete handler to each element as you add it. The elements that don't exist when you apply it on document load will never have it applied otherwise. Also, you'd be better off creating the row and input separately. By doing that you could just use the reference to the newly created input and use it with the autocomplete plugin.

$(function() {  

    $("#button_newproduct").click(function(){
         var input = $("<input type='text' name='td_products["+counter+"]' />");
         var row = $('<tr />').append( $('<td />').append(input) );  
         $("#products_table > tbody").append(row);
         var data = "Core celectors cttributes craversing canipulation CSS cvents cffects cjax ctilities".split(" ");
         input.autocomplete(data);

    });
});
5
ответ дан 1 December 2019 в 13:33
поделиться

You're adding a new input when you click #button_newproduct but you're only adding the autocomplete once in the function, outside the click.

Check jquery live

0
ответ дан 1 December 2019 в 13:33
поделиться