Javascript 如何在AJAX响应后刷新选择选项列表
我使用fastSelect插件 我有输入标记,当更改值时,我调用ajax到php服务器。 我的问题是,当我检查项目并查看选项时,我看到了我刚才添加的数据,但在浏览器中它不会显示,对不起,我的英语质量太高了 需要帮忙吗 随信附上我的密码Javascript 如何在AJAX响应后刷新选择选项列表,javascript,jquery,ajax,Javascript,Jquery,Ajax,我使用fastSelect插件 我有输入标记,当更改值时,我调用ajax到php服务器。 我的问题是,当我检查项目并查看选项时,我看到了我刚才添加的数据,但在浏览器中它不会显示,对不起,我的英语质量太高了 需要帮忙吗 随信附上我的密码 <select id="Recherche_log_commune" name="Recherche[log_commune][]" class="multipleSelectDepCom" multiple="mul
<select id="Recherche_log_commune" name="Recherche[log_commune][]" class="multipleSelectDepCom" multiple="multiple">
<optgroup label="Communes">
</optgroup>
</select>
<script> $('.multipleSelectDepCom').fastselect({maxItems: 10,noResultsText: 'Pas de résultat'}); </script>
<script>
$("#leftBlockDashboard .fstQueryInput").on("change paste keyup", function(event) {
event.preventDefault();
getCommuneAndDepartement($(this).val());
});
function getCommuneAndDepartement(expression) {
var dataString = {expression: expression};
$.ajax({
url: '{{path('get_commune_departement')}}',
type: "POST",
data: dataString,
success: function(data){
$("#Recherche_log_commune").find('optgroup[label="Communes"]').empty();
$.each(data, function(){
var option = '<option value="'+ this.com_id +'">'+ this.com_libelle +'</option>';
$("#Recherche_log_commune").find('optgroup[label="Communes"]').append(option);
});
$('.multipleSelectDepCom').fastselect({
maxItems: 10,
noResultsText: 'Pas de résultat',
});
}
})
}
</script>
$('.multipleSelectDepCom').fastselect({maxItems:10,noResultsText:'Pas de résultat'});
$(#leftBlockDashboard.fstQueryInput”).on(“更改粘贴键”,函数(事件){
event.preventDefault();
getCommuniteAndDepartment($(this.val());
});
函数GetCommuniteAndDepartment(表达式){
var-dataString={expression:expression};
$.ajax({
url:“{path('get_communie_department')}}”,
类型:“POST”,
数据:dataString,
成功:功能(数据){
$(“#Recherche_log_communite”).find('optgroup[label=“communites”]”)。empty();
$.each(数据,函数(){
var option=''+this.com_libelle+'';
$(“#Recherche_log_commune”).find('optgroup[label=“communies”]”)。append(选项);
});
$('.multipleSelectDepCom').fastselect({
最大项目:10,
noResultsText:“苏丹之旅”,
});
}
})
}
加载选项后,您需要重新连接控件:
$('.selector').fastselect();
更复杂的是(要保留选定的值):
您需要在ajax调用之后刷新select2,如下所示
setTimeout(function(){
$('#select2_id').fastselect();
},500);
好的,很粗鲁,但很有效。。。关于ajax回调,我做的是: 1) 获取对原始节点的引用。。。 就我而言是这样
var cc = $('#categories');
2) 检查是否存在.fstElement
var fsexist = $(".fstElement").length > 0;
3) 如果存在,请删除它并重新结束原始节点
if (fsexist) {
$('.fstElement').remove();
$('#categories_div').append(cc);
}
4) 雷尼特快速选择
$('#categories').fastselect({maxItems: 10,
noResultsText: 'Choose categories'});
是的,它将加载,但无法重新加载againI尝试了所有操作,仅此有效…fastselect是否有可能无法重新加载自身?
$('#categories').fastselect({maxItems: 10,
noResultsText: 'Choose categories'});