Adding options to a <select> using jQuery?

后端 未结 30 2156
萌比男神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:21

    If the option name or value is dynamic, you won't want to have to worry about escaping special characters in it; in this you might prefer simple DOM methods:

    var s= document.getElementById('mySelect');
    s.options[s.options.length]= new Option('My option', '1');
    

提交回复
热议问题