将项目附加到下拉 jQuery / Ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19435934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:57:18  来源:igfitidea点击:

Append items to dropdown jQuery / Ajax

jqueryajaxjsoncodeigniterpost

提问by David

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);),但它似乎没有附加到下拉列表(在 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();

}

采纳答案by David

Before the $.each , insert this :

在 $.each 之前,插入:

 data = $.parseJSON(data);

This made it work.

这使它起作用。

回答by shamcs

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

machineyntax.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 数据并附加到 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

如何删除以前的附加值

回答by Sushil Kumar Singh

Try this :-

尝试这个 :-

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

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

回答by Weslyn

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

它会很好用