Append items to dropdown jQuery / Ajax

2020-07-17 16:18发布

Is this the correct way to append option values to a dropdown? I am getting data back from ajax (tested it with alert(data);), but it seems that it doesn't get appended to dropdown (generated in jQuery).

$(document).on('focusout', '.generate', function(InputField) 
{
    var name = ($('.generate').val());
    $.post("<?php echo site_url('project/testFunction'); ?>",
    {
        name: name,                                
    },
    function(data, status) 
    {
        var items="";

        $.each(data, function(index, item)
        {
            items += "<option>" + item.Description + "</option>";
        });

        $("#typeSoftware").append(items);
    });
});

Generated dropdown:

$('#hardsoft tr:last').after('<tr><td>Software : </td><td>
  <select id="typeSoftware" class"add" name="softwarenames[]"/></td></tr>');

Function in controller:

public function testFunction()
{
    $name = trim($this->input->post('name'));
    $this->load->model('mProject');
    $test = $this->mProject->testFunction($name);

    echo json_encode($test);
}

Result :

enter image description here

DB Function :

 function testFunction($id) {

    $query = $this->db->get_where('R_InstalledItems', array('Description' =>$id));
    return $query->result();

}

4条回答
2楼-- · 2020-07-17 16:54

$(function()
{
var items="";
$.getJSON("adminlist/classdropdown.php",function(data)
{
$.each(data,function(index,item) 
{
items+="<option value='"+item.classname +"'>"+item.classname +"</option>";
});

$("#eclass").append(items);
});
});  

It will work good

查看更多
【Aperson】
3楼-- · 2020-07-17 16:55

Check on this example which work on my development testing.

<script type="text/javascript">
    $(document).ready(function () {

        $("#dropdownCountry").change(function (e) {
            var obj1 = { Country: $("#dropdownCountry").val() };
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "http://localhost:3323/AjaxWebService.asmx/getState",
                data: JSON.stringify(obj1),
                dataType: "json",
                success: function (data1) {
                    var appenddata1 = "";
                    //alert(data1.d);
                    var jsonData1 = JSON.parse(data1.d);
                    for (var i = 0; i < jsonData1.length; i++) {
                        appenddata1 += "<option value = '" + jsonData1[i].SHORT_NAME + " '>" + jsonData1[i].FULL_NAME + " </option>";
                    }
                    $("#dropdownState").append(appenddata1);
                }
            });
        });
    });
</script>

machinesyntax.blogspot.my/2014/01/how-to-append-dropdownlist-using-jquery.html

The idea is read JSON data from webservice and append to the dropdownlist .

And dont forget to remove the previous append value if the selection change. You can refer on this post also.

How to remove previous append value

查看更多
4楼-- · 2020-07-17 17:02

Try this :-

    function(data, status) 
    {
        var items="";

        $.each(data, function(index, item)
        {
            $("#typeSoftware").append("<option>" + item.Description + "</option>");
        }); 
    });
查看更多
Root(大扎)
5楼-- · 2020-07-17 17:12

Before the $.each , insert this :

 data = $.parseJSON(data);

This made it work.

查看更多
登录 后发表回答