Programmatically create select list

后端 未结 8 2200
不知归路
不知归路 2020-11-29 02:48

Does anyone know of a technique to programmatically create an HTML select list including options using JQuery?

相关标签:
8条回答
  • 2020-11-29 03:30

    I think it's simpler.IMHO.

     arr.map(x => ($("YOUR SELECTOR").append(`<option value="${x}">${x}</option>`)));
    
    0 讨论(0)
  • 2020-11-29 03:31

    If you have already <select> list somewhere in DOM, reuse it and make it empty from previous user interactions...

    // Call existing list, chain empty()
    var my_list = $("#my_list").empty();
    
    // Build list
    $(my_arr).each(function() {
     my_list.append($(<option>").attr(\'value\',this.item_id).text(this.item_name));
    });
    
    0 讨论(0)
提交回复
热议问题