我从ajax获取选项,如下所示。此下拉列表取决于另一个下拉菜单>。以下示例仅用于在onload上附加选项,它可以完成工作。但是,在第二个示例中,当我尝试在选择其他选项时更改选项时,只是保留附加选项而不是删除现有选项。如何根据所选值删除现有选项来生成新选项?
enter code here
//First Example
openPage: function () {
$.ajax({
type: "GET",
dataType: "json",
url: _config.GetCampaignsByMarket,
data: {
marketValue: $('#market-select').val()
},
//contentType: 'application/json',
success: function (data) {
var items = '';
$.each(data, function (key, val) {
$('.select-ajax').append('<option selected value="' + val.CampaignInitiative + '">' + val.CampaignInitiative + '</option>');
//items += "<option value='" + val.CampaignInitiative + "'>" + val.CampaignInitiative + "</option>";
});
$('.select-ajax').multiselect('rebuild');
},
error: function (xhr, status, error) {
}
});
}
//第二个例子
initSelect:function(){ var data = {};
$('#market-select').change(function () {
//$('#campaign-select').empty();
var selected = $(this).val(); // get current dropdown element selected value
$.ajax({
type: "GET",
dataType: "json",
url: _config.GetCampaignsByMarket,
data: { marketValue: $('#market-select').val() },
success: function (data) {
var items = '';
$.each(data, function (key, val) {
// items += "<option value='" + val.CampaignInitiative + "'>" + val.CampaignInitiative + "</option>";
$('.select-ajax').append('<option selected value="' + val.CampaignInitiative + '">' + val.CampaignInitiative + '</option>');
});
$('.select-ajax').multiselect('rebuild');
$('.select-ajax').multiselect('updateButtonText', false);
},
});
})
}