Your question is answered with a Jquery implementation and an HTML post for Spring MVC to handle your choices, but is not really related to Spring MVC or Bootstrap.
Why, once you’ve loaded your select
with the required fields, and an "Add" button next to it, the chosen value will be sent to a form
initially empty, which, when submitted, sends the chosen codes to the page responsible for treating them. In addition it is clear to insert visually on the screen for the user as feedback and remove them from select
.
<p>Para onde você quer ir?</p>
<select id="meu_select">
<option value="1">Paris</option>
<option value="2">Moscou</option>
<option value="3">Roma</option>
<option value="4">Viena</option>
</select>
<button onclick="minha_funcao();">Adicionar</button>
<p>Você já escolheu:</p>
<div id="escolhas">
<!-- Inicialmente vazio! -->
</div>
<form id="meu_form" method="post" action="/pagina_responsavel.php">
<input type="submit" value="Submeter escolhas">
</form>
<script>
function minha_funcao() {
if($('#meu_select').val()) {
// Adiciona no post uma variável que tem nome criado a partir do
// valor escolhido (id_...) e o valor escolhido
$('#meu_form').append('<input type="hidden" name="id_' +
$('#meu_select').val() + '" value="' + $('#meu_select').val() + '">');
// Adiciona na tela o valor escolhido
$("#escolhas").append("<p>" + $('#meu_select option:selected').text() + "</p>");
// Remove do select o valor escolhido
$("#meu_select option[value=\""+$('#meu_select').val()+"\"]").remove();
}
}
</script>
The easiest way to know what value(s) was (were) chosen(s), is to check whether for each value sent to the select
there is a post variable id_value
, where value
is 1, 2, 3, 4, etc.