To set a select field with a preset value

Asked

Viewed 3,743 times

0

I need to have the select field bring me the information that is in <%=addressable.getState()%> in which it returns "SP". In the input for city, I can get the value of <%=addressable.getCity()%> in which returns "Mogi das Cruzes" and so brings in the input this value already filled. I can’t do this for status, using value="<%=addressable.getState()%>" as in city, the state field is not returning anything, it simply goes into the first option:"select an option". Any hints on how I can take the value of <%=addressable.getState()%> and fill it automatically in select?

Note: the data contained in <%=addressgetEstado()%> and <%=addressgetCidade()%> are of the String type.

Follow the code below:

<tr>
<td><label for="cidade">Cidade:</label></td>
<td><input size="20" type="text" name="cidade" value="<%=endereco.getCidade()%>"></td>
<td><label for="uf">Estado </label></td>
<td><select type="text" name="uf" value="<%=endereco.getEstado()%>"/> 
        <option value="">Selecione uma opção...</option>
        <option value="AC">AC</option> 
        <option value="AL">AL</option> 
        <option value="AM">AM</option> 
        <option value="AP">AP</option> 
        <option value="BA">BA</option> 
        <option value="CE">CE</option> 
        <option value="DF">DF</option> 
        <option value="ES">ES</option> 
        <option value="GO">GO</option> 
        <option value="MA">MA</option> 
        <option value="MT">MT</option> 
        <option value="MS">MS</option> 
        <option value="MG">MG</option> 
        <option value="PA">PA</option> 
        <option value="PB">PB</option> 
        <option value="PR">PR</option> 
        <option value="PE">PE</option> 
        <option value="PI">PI</option> 
        <option value="RJ">RJ</option> 
        <option value="RN">RN</option> 
        <option value="RO">RO</option> 
        <option value="RS">RS</option> 
        <option value="RR">RO</option> 
        <option value="SC">SC</option> 
        <option value="SE">SE</option> 
        <option value="SP">SP</option> 
        <option value="TO">TO</option> 
    </select>
</td>

2 answers

2


I don’t really remember jsp But that’s the idea

if(endereco.getEstado().equals("sp")){ 
  //manda printar o html aqui
  //exemplo
  <option value="<%=endereco.getEstado()%>" selected>SP</option> 
}

  • It worked. I didn’t need to use any if. Only since it returns only the acronym, I changed all other options to acronyms as well. The command that worked: <select type="text" name="Uf" value="<%=addressable.getEstad()%>" id=Uf required/> <option value="<%=addressable.getEstat()%>"><%=addressable.getEstat()%></option>

  • Thanks for your help!

  • Relax, sorry for the confusion, I’m new here.

-1

Use html to leave an option marked

<select name="estado">
  <option value="sp" selected>São Paulo</option>
</select>
 

  • the data contained in <%=addressable.getEstado()%> may vary, because it is - if a result of a database query, this code you posted does not help much, as it would bring a fixed option

  • makes an if then.

  • makes an if then by checking the value that Voce wants to select.

Browser other questions tagged

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