1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| function addselect(service, parent, elt){
elt.nextAll().remove();
if ( elt.val() != '-1' ) {
$.ajax({
type: 'POST',
url: 'ajax.php',
data: 'serviceId='+service+'&parentId='+parent+'&structures=ok',
dataType: 'json',
cache: false,
beforeSend: function(){
$("<img/>",{src: "assets/img/ajax-loader.gif", id: "loader"}).insertAfter(elt);
},
complete: function(){
$("#loader").remove();
},
success: function(data){
if ( data.result.children.length > 0 ) {
var items = data.result.children, level = data.result.level;
if ( level > 0 ) {
$('<div/>',{id:'localisation','class':'row'}).insertAfter(elt);
}
if ( $('#localisation').length > 0 ) $('<select/>',{'class':'form-control'}).insertAfter('#localisation');
else $('<select/>',{'class':'form-control'}).insertAfter(elt);
$('<option/>',{value: '-1', html: 'Choisir une structure dans "' + elt.children('option:selected').text() + '"'}).appendTo($('#structuresWrap > select').eq(-1));
for(var i in items){
$("<option/>",{value: items[i].struc_id, html: items[i].struc_name}).appendTo($('#structuresWrap > select').eq(-1));
}
}
}
});
}
}
$('#structuresWrap').on('change', '> select', function() {
addselect($('input[type=radio][name=services]:checked').attr('value'), $(this).children('option:selected').attr('value'), $(this));
}); |
Partager