简体   繁体   中英

Enable autocomplete when loaded by AJAX

trying to get my head around this problem:

I often load fragments of page via .load() function such as:

<div class="fragment_load">
    <form>
    <input id="typeaway" type="text" class="autocomplete"/>
    <label>Test Type</label>
    </form>

    <script type="text/javascript">
    $("#typeaway").autocomplete({
        serviceUrl: "/restaway",
        minChars: 3,
        paramName: "query",
    });
    </script>
</div>

Now as you can imagine autocomplete is not working due to this.

I am unsure how to make it work going forwards for these small fragments that i load via AJAX.

Any assistance is appreciated.

In this case you could put the autocomplete initialisation code within the callback of the load() method, something like this:

$('#foo').load('bar.html', function() {
    $("#typeaway").autocomplete({
        serviceUrl: "/restaway",
        minChars: 3,
        paramName: "query",
    });
});

You can try below code:-

Just put it inside document.ready.

$(document).ready(function () {
 $("#typeaway").autocomplete({
        serviceUrl: "/restaway",
        minChars: 3,
        paramName: "query",
    });
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM