Pass text from a select to an input

1

I am trying that when selecting a value of a select, when opening a modal, the value and the text of the select are passed to different inputs. What would be the following:

rut_usu = this input is passed the value of the selected option.

jc = this input is passed the text of the selected option.

The value is correctly passed to the input rut_usu, but not the text to jc, this tells me "undefined". I'm bringing my values from my database.

I put an alert and it throws me all the text of the select

Function of the model showing the data of the select

 public function get_usuarios() {

  $this->db->select('rut_usu as rut, pnombre as nom,apellido_pa as pat, apellido_ma as mat');
  $this->db->from('usuarios');
  $this->db->where('id_tip=2');
  $this->db->order_by('pnombre', 'asc');
  $usuarios = $this->db->get();

  if($usuarios->num_rows() > 0 ){

  return $usuarios->result();

    }
  }

The select displayed in the view

    <div class="col-md-4">
    <div class="form-group">
      <label>Jefes de Carrera</label>
      <?= form_open(base_url().'mCalendar/get_usuarios'); ?>
       <select class="form-control" id="rut_jc" name="rut_jc">
         <option>--Seleccione un Jefe de Carrera--</option>
         <?php 
                 $this->load->model('mCalendar');
                 $usuarios = $this->mCalendar->get_usuarios();

                 foreach($usuarios as $fila){

                 echo '<option value="'. $fila->rut.'">'. $fila->nom .' '. $fila->pat .' '.$fila->mat.'</option>';

           }
            ?>

       </select>
    </div>
  </div>

Javascript

  $("#rut_jc").change(function(){

  var op=document.getElementById("rut_jc");
  var tt=document.getElementById("rut_usu"); 
  var jc=document.getElementById("jc");

  //alert($("#rut_jc").text()); 

  if (op.selectedIndex > 0)tt.value= op.value ; 
  if (op.selectedIndex > 0)jc.value= op.text ; 

  });
    
asked by Kvothe_0077 01.10.2017 в 21:17
source

1 answer

1

Good day Kvothe_0077

Using the JQuery to obtain and put the values. try with:

  $("#rut_jc").change(function(){

  var op=document.getElementById("rut_jc");

  if (op.selectedIndex > 0)$("#rut_usu").val("#rut_jc".val());
  if (op.selectedIndex > 0)$("#jc").val($( "#rut_jc option:selected" ).text()) ; 


  });

Greetings

    
answered by 01.10.2017 / 21:34
source