Adding options to a <select> using jQuery?

后端 未结 30 2175
萌比男神i
萌比男神i 2020-11-22 03:56

What\'s the easiest way to add an option to a dropdown using jQuery?

Will this work?

$(\"#mySelect\").append(\'
30条回答
  •  Happy的楠姐
    2020-11-22 04:27

    var select = $('#myselect');
    var newOptions = {
                    'red' : 'Red',
                    'blue' : 'Blue',
                    'green' : 'Green',
                    'yellow' : 'Yellow'
                };
    $('option', select).remove();
    $.each(newOptions, function(text, key) {
        var option = new Option(key, text);
        select.append($(option));
    });
    

提交回复
热议问题