JQuery Assign JSON Object to Select element

From CoderLearner
Jump to: navigation, search

Description

  • How to get JSON object, access JSON object and assign it to SELECT element

Syntax

<select>
 <option value=""></option>
</select>

JSON Object

{"code":1,"message":"Successfully Request Data!","data":[{"ID":22,"NAME":"this is"},{"ID":23,"NAME":"dfg"},{"ID":24,"NAME":"dog"},{"ID":41,"NAME":"sdfsdfsdfsdf"},{"ID":43,"NAME":"xx"},{"ID":21,"NAME":"datata"},{"ID":25,"NAME":"apps4"},{"ID":6,"NAME":"bab"}]}

JQuery

$.ajax({
		url :list.modules.url,
		type : "GET",
		data : 'func=getlist',
		dataType : "json",
		success : function(datat) {
			if (datat.code === 1) {
				var data = datat.data;
				$.each(data,function(key,value){
				 	$(list.modules.id).append($('<option></option>').attr('value',value.ID).text(value.NAME));
					 
				});
				console.log('wow!!! loading modules');
			}
		}
	});