Adding options to a <select> using jQuery?

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

    $(function () {
         var option = $("");
         option.text("Display text");
         option.val("1");
         $("#Select1").append(option);
    });
    

    If you getting data from some object, then just forward that object to function...

    $(function (product) {
         var option = $("");
         option.text(product.Name);
         option.val(product.Id);
         $("#Select1").append(option);
    });
    

    Name and Id are names of object properties...so you can call them whatever you like...And ofcourse if you have Array...you want to build custom function with for loop...and then just call that function in document ready...Cheers

提交回复
热议问题