Programmatically create select list

后端 未结 8 2262
不知归路
不知归路 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:29

    Here is another version of an answer to the question using ajax to fetch a json response used to create the select list with key value pairs

            $.ajax({
            type: 'post',
            url: 'include/parser.php',
            data: {                     
                mode: 'getSubtypes',
                type: type
            },
            success: function (response) {
                var mySubtype = document.getElementById("Component");
                var components = $.parseJSON(response);
    
                var selectList = document.createElement("select");
                selectList.id = "subtype";
                selectList.name = "subtype";
                mySubtype.appendChild(selectList);
                $('#subtype').append('');
                $.each(components, function(k, v) {
                    var option = new Option(v, k); 
                    $('#subtype').append($(option));               
                });
            }
        });        
    

提交回复
热议问题