How to get the "name" and not the "id" in jQuery ..? From MYSQL

1

I am making an order form, I use jquery, in this case, after completing the form, I added a "check data" button in which I send all the data to another form with:

  var TratamientoOD = $('#TratamientoOD').val()
  var familiaOI = $('#familiaOI').val()
  var disenyoOI = $('#disenyoOI').val()
  var pasilloOI = $('#pasilloOI').val()
  var indiceOI = $('#indiceOI').val()
  var materialOI = $('#materialOI').val()
  var TratamientoOI = $('#TratamientoOI').val()

The "problem" that arises is that these data, I take them from the database, with what in the value, the ID is really storing me (Which is necessary, to be able to do the nested select)

The case, that when I send the data according to the jquery above, it stores all the ID's, and I would like to know if there is any way to store the "Name"

That is, if you choose "hello" in the select, it does not leave ID 1, but you can see "hello" when assigning the field to the new field.

  var TratamientoOD = $('#TratamientoOD').val()

I appreciate the help of everyone as always.

    
asked by Javier Avila Fernandez 20.08.2018 в 22:50
source

1 answer

1

If what you want is the text of the selected option you can use .text() like this:

$('#TratamientoOD').change(function() {

var TratamientoOD ="Valor vacio";
if($('#TratamientoOD').val()) {
  TratamientoOD = $('#TratamientoOD option:selected').text();
}
console.log(TratamientoOD);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="TratamientoOD">
<option value="">Seleccionar</option>
<option value="1">Opcion 1</option>
<option value="2">Opcion 2</option>
    
answered by 20.08.2018 / 22:52
source