Adding options to a <select> using jQuery?

后端 未结 30 2164
萌比男神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条回答
  •  萌比男神i
    2020-11-22 04:12

    This did NOT work in IE8 (yet did in FF):

    $("#selectList").append(new Option("option text", "value"));
    

    This DID work:

    var o = new Option("option text", "value");
    /// jquerify the DOM object 'o' so we can use the html method
    $(o).html("option text");
    $("#selectList").append(o);
    

提交回复
热议问题