将项目附加到下拉列表jQuery / Ajax

时间:2022-05-18 14:14:00

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).

这是将选项值附加到下拉列表的正确方法吗?我从ajax获取数据(使用alert(data)测试它;),但它似乎没有附加到dropdown(在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 :

结果:

将项目附加到下拉列表jQuery / Ajax

DB Function :

数据库功能:

 function testFunction($id) {

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

}

4 个解决方案

#1


4  

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

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 .

这个想法是从webservice读取JSON数据并附加到下拉列表。

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

如何删除以前的附加值

#2


0  

Try this :-

尝试这个 :-

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

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

#3


0  

Before the $.each , insert this :

在$ .each之前,插入以下内容:

 data = $.parseJSON(data);

This made it work.

这使它工作。

#4


0  

$(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

它会很好用

#1


4  

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

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 .

这个想法是从webservice读取JSON数据并附加到下拉列表。

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

如何删除以前的附加值

#2


0  

Try this :-

尝试这个 :-

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

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

#3


0  

Before the $.each , insert this :

在$ .each之前,插入以下内容:

 data = $.parseJSON(data);

This made it work.

这使它工作。

#4


0  

$(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

它会很好用