如何删除以前添加的选项以容纳新的选项列表。
我使用append而不是html的原因是html在使用选项时似乎不喜欢数组。
这是我到目前为止的代码:
$('.divi_option').on('change',function(){
var option = $('.divi_option option:selected').text();
var rm_name;
$('.rm_option').prop("disabled",false);
switch(option){
case 'East':
rm_name=['Russ Martin','Carey Fischer','Brandon Born','Joe Tocyloski','Phil Hemery','Bob Mancini','Damien Ramondo','Kevin Gang','Andrew Fischer','David Saslowsky','Robert Brazofsky','Joseph Proscia','William Marsalise','Dan Stack','James Broderick'];
break;
case 'South':
rm_name=['Chris Carrelha','Don Connell','Jay O’ Connor','Dwight Cornell','Eric Indovina','Russ Corby','Chris Boeker','Robert Nelms','Joe Dominguez','Marc Della Pia','Chris Carrelha','Terry Harris'];
break;
case 'West':
break;
default:
}
for(var i=0;i<rm_name.length;i++){
$('.rm_option').append('<option>'+rm_name[i]+'</option>');
}
});
我只需要删除之前选择中添加的选项。我怎么能这样做?
答案 0 :(得分:1)
将选项存储在空的jQuery集合中,并使用html()
将其设置为一次而不是在循环内部
$('.divi_option').on('change', function () {
var option = $('.divi_option option:selected').text();
var rm_name = [];
var options = $([]);
$('.rm_option').prop("disabled", false);
switch (option) {
case 'East':
rm_name = ['Russ Martin', 'Carey Fischer', 'Brandon Born', 'Joe Tocyloski', 'Phil Hemery', 'Bob Mancini', 'Damien Ramondo', 'Kevin Gang', 'Andrew Fischer', 'David Saslowsky', 'Robert Brazofsky', 'Joseph Proscia', 'William Marsalise', 'Dan Stack', 'James Broderick'];
break;
case 'South':
rm_name = ['Chris Carrelha', 'Don Connell', 'Jay O’ Connor', 'Dwight Cornell', 'Eric Indovina', 'Russ Corby', 'Chris Boeker', 'Robert Nelms', 'Joe Dominguez', 'Marc Della Pia', 'Chris Carrelha', 'Terry Harris'];
break;
case 'West':
break;
default:
}
$.each(rm_name, function (_, item) {
options = options.add(
$('<option />', {
text: item
})
);
});
$('.rm_option').html(options);
});