如何用ajax在选择2下拉更新数据 [英] how to update data in select2 dropdown using ajax
问题描述
我有一个选择2下拉的location.select2数据初始化页load.I想使用ajax.But以定期更新数据,当我更新的选择二中选择2下拉变为只读
数据 的jQuery(#e10_2)。选择二({
allowClear:真正的,
minimumInputLength:1,
数据:{结果:locationls,文本:函数(项目){返回item.text; }},
formatSelection:格式,
formatResult:格式
});
我真的不明白你的问题,并认为它需要一些澄清 - 但据我能理解(我遇到了这个问题,并找到你的问题时, Googleing吧...)
当我第一次加载我运行下面的JavaScript,使我所有的下拉选择框的样式和页面使用选择2:
$('选择')选择2()。
但作为你的头衔状态,下面的作品我的code通过更新下拉到一个新的数据集见附我的 /存储/ ajax_get_zones
函数返回HTML的选项:
$(函数(){
$('#国)。在('改变',函数(){
$。员额(/存储/ ajax_get_zones,{
COUNTRY_ID:$('#国)VAL()。
},功能(五){
如果(E)
$(#状态)HTML(五).select2();
})
});
});
所有你需要做的就是调用 .select2()
后,更新的数据元素。
屏幕快照
开始使用默认美国:
然后,当你改变国家状态下拉的变化,以及(AJAX更新的内部HTML原来的选择,同时还为主题,以选择2
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 becomes read only
jQuery("#e10_2").select2({
allowClear: true,
minimumInputLength: 1,
data:{ results: locationls, text: function(item) { return item.text; }},
formatSelection: format,
formatResult: format
});
I 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
这篇关于如何用ajax在选择2下拉更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!