的問題是,您使用.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");
}
希望這對你的作品。
Ajax調用不被緩存。所選擇的值將與前面的附加值相加。需要清除之前附加的值並添加從按鍵事件中獲得的新值(ajax調用) – selva