JQuery UI Autocomplete 动态输入框调用方法报错问题求助
Hey Miguel, I’ve run into this exact issue before—static inputs work great with Autocomplete, but dynamic ones throw errors because they don’t exist when your initial initialization code runs. Let’s break down why this happens and how to fix it.
Why the Error Happens
When your page loads, your static <input> is already in the DOM, so calling .autocomplete() on it works perfectly. But when you generate inputs dynamically (after user interaction), those elements aren’t present when your original Autocomplete setup runs. If you try to call .autocomplete() before the element is added to the DOM, or on a selector that doesn’t match anything yet, the browser will throw an error like "Cannot read property 'autocomplete' of undefined".
Step-by-Step Solutions
1. Initialize Autocomplete Right After Creating the Element
The simplest fix is to call .autocomplete() immediately after you add the dynamic input to the DOM. This ensures the element exists when you try to attach the plugin.
Example code:
// Let's say this is your code to add a dynamic input $('#add-input-button').click(function() { // Create the new input element const $newInput = $('<input type="text" class="dynamic-autocomplete">'); // Add it to your form/container $('#form-container').append($newInput); // Initialize Autocomplete on the NEW element immediately $newInput.autocomplete({ source: ['Lemon', 'Orange', 'Grape'], // Your data source (array/URL) minLength: 1, // Optional: minimum characters to trigger suggestions select: function(event, ui) { // Optional: custom logic when a suggestion is selected $newInput.val(ui.item.label); return false; } }); });
2. Use a Reusable Initialization Function
If you’re generating multiple dynamic inputs, wrap the Autocomplete setup in a reusable function to keep your code clean. This way you can call it for both static and dynamic inputs.
Example code:
// Reusable function to initialize Autocomplete function setupAutocomplete($inputElement) { $inputElement.autocomplete({ source: '/api/fruit-suggestions', // Example: remote data source minLength: 2, open: function() { // Optional: custom logic when the suggestion menu opens } }); } // Initialize static input on page load $(document).ready(function() { setupAutocomplete($('#static-input')); }); // Initialize dynamic input when it's created $('#add-input-btn').click(function() { const $newDynamicInput = $('<input type="text" class="dynamic-input">'); $('#form').append($newDynamicInput); // Call the reusable function for the new input setupAutocomplete($newDynamicInput); });
3. Avoid Common Pitfalls
- Don’t use a broad selector like
$('.dynamic-input').autocomplete()every time you add a new input—this will re-initialize Autocomplete on all existing inputs, which can cause bugs or performance issues. Always target the specific new element you just created. - Double-check that jQuery and jQuery UI are loaded before any of your initialization code runs (since your static input works, this is probably already handled, but it’s worth confirming).
Quick Troubleshooting
If you’re still seeing errors:
- Open your browser’s DevTools (F12) and check the Console tab for the exact error message. If it says "$.autocomplete is not a function", make sure jQuery UI is loaded after jQuery.
- Verify that the dynamic input is actually being added to the DOM before you call
.autocomplete()—you can inspect the DOM in DevTools to confirm.
内容的提问来源于stack exchange,提问作者Miguel Lara

