jquery auto complete for dynamically generated textboxes

后端 未结 5 2098
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 11:45

i am new to jquery, i am working on a web page which needs generating text boxes dynamically with autocomplete facility.

i tested $(\"#some\")

相关标签:
5条回答
  • 2020-12-05 12:02

    The main issue i think is that you are calling the autocomplete outside of the click handler. So the autocompletion gets set up when the page loads, rather than when the button is clicked.

    To resolve this, alter the code to read:

    $(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);
    
        });
    });
    

    Now the autocompletion is being set on each new item, rather than once, at the start.

    0 讨论(0)
  • 2020-12-05 12:06

    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);
    
        });
    });
    
    0 讨论(0)
  • 2020-12-05 12:17

    Some thoughts:

    • Where is counter initialized?
    • Try leaving a space between the 'type' and 'name' attributes of your input tags, <input type='text' name='td_products... >, that might be preventing your startsWith attribute filter from matching anything.
    0 讨论(0)
  • 2020-12-05 12:18

    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 讨论(0)
  • 2020-12-05 12:22

    The simplest way is to use:

    $( ".some" ).autocomplete({source: data});
    

    inside the javascript function who generates the new element.

    0 讨论(0)
提交回复
热议问题