// Ao clicar em #empresa abre a caixa de diálogo com as opções.
$(document).on('click', '#empresa', function(){
// Limpa os nomes da caixa de dialogo que foram colocados na vez anterior.
$('#nomes').empty();
// Simulação de requisição ajax para pegar a lista de opções como se fosse pelo server-side.
$.ajax({
type:"POST",
url:"/echo/json/",
data:{
json: JSON.stringify(
[
{'nome' : 'Matriz'},
{'nome' : 'Filial 1'},
{'nome' : 'Filial 2'}
]
)
},
// Em caso de sucesso na resposta da requisição, colocamos os nomes dentro da lista não ordenada (com id #nomes)
success:function(response) {
$(response).each(function(i,v){
$('#nomes').append('<li><a href="#">'+v.nome+'</a></li>');
});
}
});
// Abre a janela modal e configura as opções
$('#modal').dialog({
title: 'Opções',
modal: true,
draggable: false,
// position: { my : 'center', at : 'center', of : window },
open: function(event, ui) {
$(".ui-dialog-titlebar-close", $(this).parent());
var nomes = $('#nomes');
$('.ui-dialog-content').html(nomes);
$(document).on('click', '#nomes li a', function(){
$('#empresa').val($(this).text());
$('#modal').dialog('close');
});
$('.ui-widget-overlay').on('click', function(){
$("#modal").dialog('close');
});
}
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.