Created
October 9, 2015 21:42
-
-
Save zmajstor/17c96923b35481835c44 to your computer and use it in GitHub Desktop.
call ajax on SELECT change (in rails view)
This file contains 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
<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