Pass value from a select to modal select

2

Good I'm doing a form edition through modal. I have managed to pass all the necessary data through javascript, but I do not know how to pass the value of a select to the modal's selection.

The select is the state of Activated, Deactivated, and in the Bd the Activated is the value 1 and deactivated the value 0.

I'll explain how to send and receive the data in the modal:

This is one of the many data I send, but I put only one to not fill the post much.

<td class="tdfgaz" id="Name<?php echo $res['IdUsuario']; ?>"><?php echo $res['Nombre']; ?></td>

With this SCRIPT I send the data to the modal:

<script>
        $(document).ready(function(){
            $(document).on('click', '.edit', function(){
                var id=$(this).val();
                var IdUsuario=$('#IdUsuario'+id).text();
                var Name=$('#Name'+id).text();
                var Nif=$('#Nif'+id).text();
                var Direccion=$('#Direccion'+id).text();
                var Poblacion=$('#Poblacion'+id).text();
                var Postal=$('#Postal'+id).text();
                var Provincia=$('#Provincia'+id).text();
                var Email=$('#Email'+id).text();
                var Telefono=$('#Telefono'+id).text();
                var Movil=$('#Movil'+id).text();
                var Web=$('#Web'+id).text();
                var Informacion=$('#Informacion'+id).text();

                $('#edit').modal('show');
                $('#eIdUsuario').val(IdUsuario);
                $('#eName').val(Name);
                $('#eNif').val(Nif);
                $('#eDireccion').val(Direccion);
                $('#ePoblacion').val(Poblacion);
                $('#ePostal').val(Postal);
                $('#eProvincia').val(Provincia);
                $('#eEmail').val(Email);
                $('#eTelefono').val(Telefono);
                $('#eMovil').val(Movil);
                $('#eWeb').val(Web);
                $('#eInformacion').val(Informacion);

            });
        });
    </script>

And in this way I print them in the modal input:

  <div class="form-group">
   <label>Nombre</label>
   <input type="text" class="form-control inputmiocont corpiii" id="eName" name="eName">
   </div>

The field of select I have it like this:

<td class="otrotdfgaz">
 <?php
   if ($res['intestado'] == 1) echo "<span class=\"conect-label\">Activado</span>"; else
   echo "<span class=\"desconect-label\">Desactivado</span>";
 ?>
</td>

I correct to put the editions:

Here the value of the BD is printed first, in a table:

<td class="otrotdfgaz" id="select2-1<?php echo $res['IdUsuario']; ?>">
  <?php
   if ($res['intestado'] == 1) echo "<span class=\"conect-label\">Activado</span>";
   else echo "<span class=\"desconect-label\">Desactivado</span>";
  ?>
</td>

The JavaScript code that sends it to the modal:

<script>
        $(document).ready(function(){
            $(document).on('click', '.edit', function(){
                var id=$(this).val();
                var Estado=$('#select2-1'+id).children('span').first().text();


                $('#select2-1').val(Estado);


            });
        });
 </script>

This is where you have to print with a selected the value that comes from the td. In addition that also this the other option to be able to change it.

<select id="select2-1" value="" class="form-control" name="estado">
  <option value="1">Activado</option>
  <option value="0">Desactivado</option>
</select>
    
asked by Miguel 15.08.2018 в 21:58
source

1 answer

2

Javascript for select

<script>
    $(document).ready(function(){
        $(document).on('click', '.edit', function(){
            var id=$(this).val();

            //Obtener status
            var Estado=$('#select2-1'+id).children('span').first().text();

            //Convertir status a numero para asignar select
            if(Estado == 'Activado')
                $('select#select2-1').val(1).trigger('change');
            else
                $('select#select2-1').val(0).trigger('change');

        });
    });

Using select2.js

    
answered by 15.08.2018 / 22:47
source