Select does not select the correct value

1

I have the following problem:

<select name="cargo" id="selectCargo" 
    class="form-control show-tick maiuscula" required >
    <option value="">ESCOLHA A EMPRESA</option>
    <c:forEach items="${filaEmpresa }" var = "filaEmpresa">
        <option value="${filaEmpresa.id}" 
            selected="${funcionario.empresa.id }">${filaEmpresa.id }
        </option>
    </c:forEach>
</select>

This my select , is not correctly selecting the ID of the employee's company.

Ex : ID is 7 and it is always selecting 10 (which is the last).

You are always selecting the last value, even if funcionario.empresa.id is the correct value.

I have already debugged and I did not find the point of error.

In this case I'm making an employee change. But this is the only field that is not correct.

    
asked by anonymous 09.12.2017 / 13:32

2 answers

1

The selected attribute is a Boolean value that indicates which of the options to select. If there are multiple tags option with the selected property, the last one will be selected.

As you want to select the employee's company, you should verify that the identifier of empresa and funcionario.empresa are equal. If so, you should add the selected property. Example:

<select name="cargo" id="selectCargo" class="form-control show-tick maiuscula" required>
    <option value="">ESCOLHA A EMPRESA</option>
    <c:forEach items="${filaEmpresa}" var="empresa">
        <option value="${empresa.id}" <c:out value="${funcionario.empresa.id eq empresa.id ? 'selected' : ''}"></c:out>>${empresa.id}</option>
    </c:forEach>
</select>
    
09.12.2017 / 14:27
0

The selected does not receive values, it serves to "setar" the default option, with each loop rotated in forEach it starts the selected in the option, so it will always be the last one selected. In order to work the way you want it, you have to do a conditional and write selected only in option that should be even selected ...

    
09.12.2017 / 13:42