Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
call ajax on SELECT change (in rails view)
<script type="text/javascript">
$(document).on("change", "#my_select", callAjax);
$.ajaxSetup({
headers: { 'X-CSRF-Token': '<%= form_authenticity_token.to_s %>' },
timeout: 30000, // timeout after 30 seconds
async: true,
});
function callAjax() {
$('#content').text('loading classes, please wait...');
$.ajax({
type: 'GET',
url: '<%= ajax_path(format: :json).html_safe %>',
dataType: "json",
data: { id: $('#my_select').val() },
}).done( function (data, status) {
// console.log(data);
$('#content').html('');
for (var i = 0; i < data.length; i++) {
var ec = data[i];
var $label = $('<label class="control-label" for="class_number_' + ec['id'] + '">' + ec['name'] + '</label>');
var $input = $('<div class="controls"><input id="class_number_' + ec['id'] + '" name="class_numbers[' + ec['id'] + ']" type="text" /></div>');
var $row = $('<div class="control-group">').append($label).append($input);
$('#content').append($row);
}
}).fail( function (data, status) {
$('#content').text("update server error: " + data.status + ' ' + data.statusText + '\n' + data.responseText);
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.