Select does not select the correct value

Asked

Viewed 35 times

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>

That one of mine select, is not correctly selecting the ID of the employee’s company.

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

Always selecting the last value, even if no funcionario.empresa.id is the correct value.

I’ve debugged and can’t find the point of error.

In this case I am performing the change of an employee. But this is the only field that is not correct.

2 answers

2


The attribute selected is a boolean value indicating which of the options should be selected. If there are multiple tags option with the property selected, the last will be selected.

As you want to select the employee company, you should check whether the employee identifier empresa and of funcionario.empresa are equal. If they are, you must add the property selected. 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>
  • It worked perfectly, thanks for the great explanation :D

0

The selected does not receive values, it serves to "set" the default option, every loop run in foreach it prints the selected option, so you will always be the last selected. In order for it to work the way you want it, you will have to make a parole and write the selected only in the option that must be even selected...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.