Change the options array of a select list

前端 未结 5 1309
青春惊慌失措
青春惊慌失措 2020-12-03 10:29

Is there a way to change the options array of an html select list using javascript or mootools?

I need to replace the entire options set with a new one. In my ajax

5条回答
  •  一向
    一向 (楼主)
    2020-12-03 10:53

    var new_options = ['Option 1', 'Option 2', 'Option 3'];
    
    /* Remove all options from the select list */
    $('yourSelectList').empty();
    
    /* Insert the new ones from the array above */
    $each(new_options, function(value) {
        new Element('option')
            .set('text', value)
            .inject($('yourSelectList'));
    });
    

提交回复
热议问题