将项目附加到下拉jQuery/Ajax [英] Append items to dropdown jQuery / Ajax

查看:86
本文介绍了将项目附加到下拉jQuery/Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是将选项值附加到下拉菜单中的正确方法吗?我正在从ajax取回数据(已通过alert(data);测试),但似乎没有将其附加到下拉列表中(在jQuery中生成).

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);
    });
});

生成的下拉列表:

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

控制器中的功能

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

    echo json_encode($test);
}

结果:

数据库功能:

 function testFunction($id) {

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

}

推荐答案

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

Before the $.each , insert this :

 data = $.parseJSON(data);

这使它起作用.

这篇关于将项目附加到下拉jQuery/Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆