Adding options to a <select> using jQuery?

后端 未结 30 1678
萌比男神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条回答
  •  感情败类
    2020-11-22 04:31

    To help performance you should try to only alter the DOM once, even more so if you are adding many options.

    var html = '';
    
    for (var i = 0, len = data.length; i < len; ++i) {
        html.join('');
    }           
    
    $('#select').append(html);
    

提交回复
热议问题