I have a form where the client needs to inform the number of ports of the patch panel, and according to the value that it populate this field, new fields appear dynamically to add other relevant information.
Example: to register a new patch and inform that it has 5 ports in the form, as soon as it is filled, then eh to display 5 new input fields so that it tells what each port will connect. I've tried a few codes but have not been successful.
My HTML form
<div class="form-group">
<label>Número de portas:</label> <input type="text"
name="patchpanel.numPortas" class="form-control"
value="${flash['patchpanel.numPortas'] ? flash['patchpanel.numPortas'] : p?.numPortas}">
<span class="alert-danger">#{error 'patchpanel.numPortas' /}</span>
</div>
<div class="inputs">
<label for="quantidade">Equipamento conectado:</label> <a href="javascript:void(0)" id="adicionarcampo"><img src="/public/images/plus.png"></a><br>
<input type="text" name="portas.descricao[]" placeholder="Informe o equipamento contectado a porta" class="form-control"/>
</div>
My jQuery:
var max_fields = 10;
var wrapper = $(".inputs");
var add_button = $("#adicionarcampo");
var x = 1;
$(document).ready(function(){
$(add_button).click(function(e) {
e.preventDefault();
var length = wrapper.find("input:text.textAdded").length;
if (x < max_fields) {
x++;
$(wrapper).append('<div><a href="#" class="remove_field">Remover</a><input type="text" name="portas.descricao['+ (length+1) +']"class="form-control" placeholder="Informe o equipamento contectado a porta" /></div>');
}
});
$(wrapper).on("click", ".remove_field", function(e) {
e.preventDefault();
$(this).parent('div').remove();
x--;
});
$("#alert-target").click(function () {
toastr["success"]("I was launched via jQuery!")
});
})
WhereitbehinddoorsasNULL