Skip to content

Instantly share code, notes, and snippets.

@jsven69gist
Created January 20, 2014 12:53
Show Gist options
  • Save jsven69gist/8519405 to your computer and use it in GitHub Desktop.
Save jsven69gist/8519405 to your computer and use it in GitHub Desktop.
jQuery: Select w/child populated with Ajax request
$('#ParentID').change(function() { // Populate selectbox #ChildID based on selected option in #ParentID
// alert( "Handler for .change() called." );
// Ajax data
var data = new Object();
data.parentID = $('#ParentID').val(); // Get value from select
// Ajax options
var options = new Object();
options.data = data;
options.dataType = 'json';
options.type = 'post';
options.url = 'ajax_script.php';
options.success = function(result) {
$('#ChildID').children().remove(); // First remove all items
if (result.length > 0) {
$("#ChildID").append('<option value="0">Choose...</option>');
}
// for (key in result) {
for (var key in result) {
$("#ChildID").append('<option value="' + result[key] + '">' + key + '</option>');
}
// $("#ChildID").selectmenu('refresh');
};
// Ajax request
$.ajax(options);
}); // End select #ParentID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment