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 :
DB Function :
function testFunction($id) {
$query = $this->db->get_where('R_InstalledItems', array('Description' =>$id));
return $query->result();
}
It will work good
Check on this example which work on my development testing.
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
Try this :-
Before the $.each , insert this :
This made it work.