简体   繁体   中英

how to use source in autocomplete in jquery?

below is the html input

<input type="text" class="autocomplete" id="search_data" name="search_data" value=""  data-link="currency_autocomplete.php" >

this is the jquery i use for autocomplete

$(function() { 
        $(".autocomplete").autocomplete({
            source: 'currency_autocomplete.php',
            minLength: 2,
            select: function( event, ui ) {
                $(this).val(ui.item.value);
                return false;}
        })
});

the above code is working fine

but i need to update jquery source: something like below

$(function() { 
        $(".autocomplete").autocomplete({
            source: $(this).attr('data-link'),  *** here i need update
            minLength: 2,
            select: function( event, ui ) {
                $(this).val(ui.item.value);
                return false;}
        })
});

this is not .autocomplete on that context and that's the reason why you can't get data-link attribute value. If there is only one element with .autocomplete class you could use this

source: $(".autocomplete").data('link')

Use this for the case you have more elements with class .autocomplete

$(".autocomplete").each(function() {
    var $this = $(this);
    $this.autocomplete({
        source: $this.data('link'),
        minLength: 2,
        select: function( event, ui ) {
            $(this).val(ui.item.value);
            return false;
        }
    });
});

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