Created
January 20, 2014 12:53
-
-
Save jsven69gist/8519405 to your computer and use it in GitHub Desktop.
jQuery: Select w/child populated with Ajax request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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