问题是您使用.append
并且从不手动清除数据。随后的 ajax 请求没有以任何方式“连接”到选择/选择框。最简单的方法是在使用之前清除所有选项$(".chzn-select").html("");
。但是,这可能会在重置时和每个新项目时导致额外的 DOM 更新。下面的代码预先收集所有选项并一次性插入它们:
// safely store original options in an array. You only need to call
// this once. It's probably best to run this before initiating chosen()
// to avoid possible conflicts.
var originalOptions = [];
$(".chzn-select").children("option").each(function(ind, elm) {
originalOptions.push(elm);
});
// init chosen after
$(".chzn-select").chosen();
// (the other code here)
success: function(data) {
var newOptions = [];
$.each(data, function(index) {
newOptions.push($('<option/>').val(data[index]).html(data[index]).get(0));
});
// this line will clear *all* entries in the select. Therefore
// we combine originalOptions and newOptions, so that the original
// options are kept. If you want the ajax options to be listed first,
// switch the variables.
$(".chzn-select").html(originalOptions.concat(newOptions));
$(".chzn-select").trigger("liszt:updated");
}
希望这对你有用。