Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

jquery - Trouble updating Bootstrap's typeahead data-source with post response

Using Bootstrap's typeahead javascript plugin, I'm attempting to change the data-source attribute via jQuery's $.post method. Initially, I have:

<input type="text" data-provide="typeahead" data-source="["Option 1","Option 2","Option 3"]">

Then, let's say a button is clicked and it tries to update the data-source:

 $('button').on('click',function(){
     $.post('update.php',function(resp){
          $('input').attr('data-source',resp);
     });
  });

The resp XHR result returns an array like this:

  ["One Option","Two Option","Three Option"]

I'm finding that this does not reliably update the data-source with a new array that was constructed in the response.

Does anyone know what the problem could be?

This does not appear to capture the selected value. Does anyone know how to get the selected value using typeahead with Bootstrap?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I eventually figured out how to do this. It is outlined on github here.

Access the typeahead input's data attribute and modify the source array directly. E.g:

var autocomplete = $('input').typeahead();

//where newSource is your own array
autocomplete.data('typeahead').source = newSource; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...