简体   繁体   中英

javascript get text option instead value

i am trying to get the value of an option with the value set. via javascript though I get the text instead of the value is there something i'm doing wrong?

<div id="scheda">
    <form name="scheda">
        <fieldset class="gruppo">
          <fieldset class="row"><label>Tipologia contratto</label>
                <select name="contratto" id="contratto" class="select">
                    <option value="0">--</option>
                    <option value="1">Contratto 1/2 giorni</option>
                    <option value="2">Contratto 3 giorni</option>
                    <option value="3">Contratto 4 giorni</option>
                    <option value="4">Contratto 5 giorni</option>
                    <option value="5">Non in attività</option>
                </select>
            </fieldset>
        </fieldset>
    </form>
</div>

this is the js code

var tipocontratto = document.scheda.contratto.value;

the console log instead of giving me the value set gives me the text of the option

 function getValue() { console.log(window.contratto.value) }
 <div id="scheda"> <form name="scheda"> <fieldset class="gruppo"> <fieldset class="row"><label>Tipologia contratto</label> <select name="contratto" id="contratto" class="select"> <option value="0">--</option> <option value="1">Contratto 1/2 giorni</option> <option value="2">Contratto 3 giorni</option> <option value="3">Contratto 4 giorni</option> <option value="4">Contratto 5 giorni</option> <option value="5">Non in attività</option> </select> </fieldset> </fieldset> </form> <button onCLick="getValue()">Get Value</button> </div>

Since contratto is an id you can access it directly via the window object.

So window.contratto.value should do the trick.

i found the problem.

the select is repeated further down to retrieve the data of a user that has already been saved, in that select the values inside the options were missing.

<fieldset class="row"><label>Tipologia contratto</label>
   <select name="contratto" class="select">
      <option <?php  if($ut->tipo_contratto=="0"){?> selected="selected" <?php  }?> >--</option>
      <option <?php  if($ut->tipo_contratto=="1"){?> selected="selected" <?php  }?> >Contratto 1-2 giorni</option>
      <option <?php  if($ut->tipo_contratto=="2"){?> selected="selected" <?php  }?> >Contratto 3 giorni</option>
      <option <?php  if($ut->tipo_contratto=="3"){?> selected="selected" <?php  }?> >Contratto 4 giorni</option>
      <option <?php  if($ut->tipo_contratto=="4"){?> selected="selected" <?php  }?> >Contratto 5 giorni</option>
      <option <?php  if($ut->tipo_contratto=="5"){?> selected="selected" <?php  }?> >Non in attivita</option>
    </select>
</fieldset>

replaced with

<fieldset class="row"><label>Tipologia contratto</label>
   <select name="contratto" class="select">
      <option value="0" <?php  if($ut->tipo_contratto=="0"){?> selected="selected" <?php  }?> >--</option>
      <option value="1" <?php  if($ut->tipo_contratto=="1"){?> selected="selected" <?php  }?> >Contratto 1-2 giorni</option>
      <option value="2" <?php  if($ut->tipo_contratto=="2"){?> selected="selected" <?php  }?> >Contratto 3 giorni</option>
      <option value="3" <?php  if($ut->tipo_contratto=="3"){?> selected="selected" <?php  }?> >Contratto 4 giorni</option>
      <option value="4" <?php  if($ut->tipo_contratto=="4"){?> selected="selected" <?php  }?> >Contratto 5 giorni</option>
      <option value="5" <?php  if($ut->tipo_contratto=="5"){?> selected="selected" <?php  }?> >Non in attivita</option>
    </select>
</fieldset>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM