below is my ajax call and i am getting r JSON data
$.ajax ({
type: "POST",
url: "getrecieverlist.php",
data: { strSenderID: sendmeid },
success: function (r) {
}
});
I am getting r response as JSON data below
{
"receiverDetails": [
{
"receiver_id": "55555",
"name": "Nitin",
"mobile": "7777777777",
"ifsc_code": "IFSC54545",
"acc_no": "16-01-2017"
},
{
"receiver_id": "66666",
"name": "Vikram",
"mobile": "9191919191",
"ifsc_code": "IFSC54545",
"acc_no": "13-01-2017"
}
],
"success": 1
}
and my select box html code is below
<select id="recieverlist" name="select" class="main-form">
<option value="">None Selected</option>
</select>
what i want is to populate select box options with text as name from the JSON data and value of the options to be receiverid, i tried many things but did not worked out for me need the proper guide how to do it?
Give a try on following.
success : function (r) {
var data = r.receiverDetails;
var options = '';
for(var i=0; i<data.length; i++) { // Loop through the data & construct the options
options += '<option value="'+data[i].receiver_id+'">'+data[i].name+'</option>';
}
// Append to the html
$('#recieverlist').append(options);
}
You can also $.each function.
$.each(data.receiverDetails, function(index, value)
$("#recieverlist").append('<option value="'+value.receiver_id+'">'+value.name+'</option>');
});