最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Autocomplete using data attribute for source? - Stack Overflow

programmeradmin0浏览0评论

Can I use data attribute for the source of my autocomplete?

for example

HTML

 <input type="text" class="autocomplete" data-source="/search.php" />

Javascript

  $(".autocomplete").autocomplete({
        source :  $(this).data('source'),
        minLength:1,
        select: function( event, ui ) {
            console.log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
    });

I tried it but it always gives me an error. What's wrong with my code?

Uncaught TypeError: Property 'source' of object #<Object> is not a function

Can I use data attribute for the source of my autocomplete?

for example

HTML

 <input type="text" class="autocomplete" data-source="/search.php" />

Javascript

  $(".autocomplete").autocomplete({
        source :  $(this).data('source'),
        minLength:1,
        select: function( event, ui ) {
            console.log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
    });

I tried it but it always gives me an error. What's wrong with my code?

Uncaught TypeError: Property 'source' of object #<Object> is not a function

Share Improve this question edited Mar 20, 2012 at 12:13 Lightness Races in Orbit 385k77 gold badges664 silver badges1.1k bronze badges asked Mar 19, 2012 at 2:43 rjmcbrjmcb 3,7459 gold badges34 silver badges47 bronze badges 2
  • This is not a good solution. On every key press you're reinitializing the autocomplete widget, which means even after moderate use, hundreds of event listeners will be added to each .autocomplete element. I strongly recommend you use another solution. (Perhaps one of the three listed below?) – Xavi Commented Mar 19, 2012 at 3:49
  • If you fixed it, write an answer. Answers do not go in questions. – Lightness Races in Orbit Commented Mar 20, 2012 at 12:14
Add a comment  | 

4 Answers 4

Reset to default 7

In source you can use this.element which refers to the input

$(".autocomplete").autocomplete({
   source :  this.element.attr('data-source'),
   minLength:1,
   select: function( event, ui ) {
   console.log( ui.item ?
            "Selected: " + ui.item.value + " aka " + ui.item.id :
            "Nothing selected, input was " + this.value );
    }
});

here's the fix

$('.autocomplete').keypress(function(){ 
    $(this).autocomplete({ 
        source: $(this).data('source'), 
        minLength: 1, 
        select: function(event, ui) { 
            console.log(ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.id : "Nothing selected, input was " + this.value); 
        } 
    }); 
}); 

I added a keypress function so that it will get the current element.

The this pointer does not refer to the .autocomplete element -- this only equals the selected element inside callbacks executed by jquery. It looks like you want to do something like this:

$(".autocomplete")
    .autocomplete({
        minLength:1,
        select: function( event, ui ) {
            console.log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
    })
    .each(function() { // Goes through `.autocomplete` elements and sets source
        $(this).autocomplete("option", "source", $(this).data('source'));
    })
;

every keystroke of autocomplete will trigger a remote request if the source is a url. what you can do to prevent that is to "pre-fetch" the data (make sure to return a JSON-valid array), then add the return data as the source for the autocomplete. that way, data is only fetched once, and autocomplete will reference to that data.

jQuery autocomplete already has a filtering capability. you just need a full list of items and it will filter it for you.

//get all input boxes with class "autocomplete"
$('.autocomplete').each(function(){

    //reference input and get it's url
    var input = $(this);
    var url = input.data('source');

    //get list array only ONCE for each input using their specified urls
    $.get(url, function(data) {

        //when request is received, add autocomplete using the returned data
        input.autocomplete({
            source: data,
            minLength: 1,
            select: function(event, ui) {
                console.log(ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.id : "Nothing selected, input was " + this.value);
            }
        });
    });
});
发布评论

评论列表(0)

  1. 暂无评论