admin管理员组文章数量:1287595
I have a select2 dropdown for location.select2 data is initialised on page load.I want to update the data at regular intervals using ajax.But when I update the data of select2 the select2 dropdown bees read only
jQuery("#e10_2").select2({
allowClear: true,
minimumInputLength: 1,
data:{ results: locationls, text: function(item) { return item.text; }},
formatSelection: format,
formatResult: format
});
I have a select2 dropdown for location.select2 data is initialised on page load.I want to update the data at regular intervals using ajax.But when I update the data of select2 the select2 dropdown bees read only
jQuery("#e10_2").select2({
allowClear: true,
minimumInputLength: 1,
data:{ results: locationls, text: function(item) { return item.text; }},
formatSelection: format,
formatResult: format
});
Share
Improve this question
asked Nov 11, 2013 at 6:14
Herald CharleyHerald Charley
1011 gold badge2 silver badges9 bronze badges
1
- Here locationls is the location Array – Herald Charley Commented Nov 11, 2013 at 12:30
1 Answer
Reset to default 8I don't really understand your question and think it needs some clarifying - but as far as I could understand (I ran into this problem and found your question when Googleing it...)
When I first load the page I run the following JavaScript so that all of my drop-down select boxes are styled and using select2:
$('select').select2();
But as your title states, my code below works by updating the drop-down to a new set of data -where my /store/ajax_get_zones
function returns HTML for the options:
$(function() {
$('#country').on('change', function() {
$.post("/store/ajax_get_zones", {
country_id: $('#country').val()
}, function(e) {
if (e)
$("#state").html(e).select2();
})
});
});
All you have to do is call .select2()
on the element after you update the data.
Screen Shots
Start with the default United States:
Then when you change the country the state drop-down changes as well (ajax updated the inner HTML of the original select while still themed to select2
本文标签: javascripthow to update data in select2 dropdown using ajaxStack Overflow
版权声明:本文标题:javascript - how to update data in select2 dropdown using ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741294952a2370763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论