pass the value of a select to another select

1

I have two views, view1 = victims and view2 = reporter that are the same only change the name and the id of the person entered 1 = victim and 3 = whistleblower.

as a complainant I have a check that when checked would have to pull the victim's data,

The script that I use to pass the data from victim to whistleblower is:

function Victima(valor) {
    if (valor == 1) {

        $("#anonimo").attr("style", "display:none") // esto es para ocultar otro check
        document.datos.PRIMER_NOMBRE.value = document.getElementById("PRIMER_NOMBREV").value
        document.datos.SEGUNDO_NOMBRE.value = document.getElementById("SEGUNDO_NOMBREV").value
        document.datos.TERCER_NOMBRE.value = document.getElementById("TERCER_NOMBREV").value
        document.datos.PRIMER_APELLIDO.value = document.getElementById("PRIMER_APELLIDOV").value
        document.datos.SEGUNDO_APELLIDO.value = document.getElementById("SEGUNDO_APELLIDOV").value
        document.datos.APELLIDO_CASADA.value = document.getElementById("APELLIDO_CASADAV").value  
        document.datos.TELEFONO_CELULAR.value = document.getElementById("TELEFONO_CELULARV").value  
        document.datos.TELEFONO_CASA.value = document.getElementById("TELEFONO_CASAV").value  
        document.datos.TELEFONO_OTROS.value = document.getElementById("TELEFONO_OTROSV").value        


    } else {

        $("#anonimo").removeAttr("style", "display:none")
    }

}

I do what I need, BUT only in the input and I have several fields that I do not pull the data

My victim select is:

<label>10. Departamento:<b style="color:red">*</b></label>
            <select class="form-control" id="DEPARTAMENTOV" name="DEPARTAMENTO" onchange="MunicipioDenunciante(this.value);" required>
                <option value="99">Ignorado</option>
                @foreach (var d in ValorDepto)
                {
                    if (d.COD_DEPARTAMENTO == DEPARTAMENTO)
                    {
                        <option value="@d.COD_DEPARTAMENTO" selected id="DEPVALOR">@d.NOMBRE</option>
                    }
                    else
                    {
                        <option value="@d.COD_DEPARTAMENTO">@d.NOMBRE</option>
                    }
                }
            </select>

and I want to reflect it in the denunica selection:

<label>10. Departamento:<b style="color:red">*</b></label>
            <select class="form-control" id="DEPARTAMENTOD" name="DEPARTAMENTO" onchange="MunicipioDenunciante(this.value);" required>
                <option value="99">Ignorado</option>
                @foreach (var d in ValorDepto)
                {
                    if (d.COD_DEPARTAMENTO == DEPARTAMENTO)
                    {
                        <option value="@d.COD_DEPARTAMENTO" selected id="DEPVALOR">@d.NOMBRE</option>
                    }
                    else
                    {
                        <option value="@d.COD_DEPARTAMENTO">@d.NOMBRE</option>
                    }
                }
            </select>

I try this without success:

  var cod = document.getElementById("DEPARTAMENTOV").value;
        document.datos.DEPARTAMENTO.val(cod);
    
asked by rmonroy 19.02.2018 в 16:28
source

2 answers

1

I understand your problem, you could try the following:

You have a check in your "Whistleblower" view, you want to pull the values of "Victim" and that the values in your Victim Select are loaded the same as the whistleblower, you just have to do the following:

Your Select in Victim:

<label>10. Departamento:<b style="color:red">*</b></label>
        <select class="form-control" id="DEPARTAMENTOV" name="DEPARTAMENTO" onchange="MunicipioDenunciante(this.value);" required>
            <option value="99">Ignorado</option>
            @foreach (var d in ValorDepto)
            {
                if (d.COD_DEPARTAMENTO == DEPARTAMENTO)
                {
                    <option value="@d.COD_DEPARTAMENTO" selected id="DEPVALOR">@d.NOMBRE</option>
                }
                else
                {
                    <option value="@d.COD_DEPARTAMENTO">@d.NOMBRE</option>
                }
            }
        </select>

You want it to be reflected in the Whistleblower view, which is an exact copy, only what'id' differences:

<label>10. Departamento:<b style="color:red">*</b></label>
        <select class="form-control" id="DEPARTAMENTOD" name="DEPARTAMENTO" onchange="MunicipioDenunciante(this.value);" required>
            <option value="99">Ignorado</option>
            @foreach (var d in ValorDepto)
            {
                if (d.COD_DEPARTAMENTO == DEPARTAMENTO)
                {
                    <option value="@d.COD_DEPARTAMENTO" selected id="DEPVALOR">@d.NOMBRE</option>
                }
                else
                {
                    <option value="@d.COD_DEPARTAMENTO">@d.NOMBRE</option>
                }
            }
        </select>

For what we can do is get the value of DEPARTMENTV - > Victim, and load it in DEPARTAMENTOD through your javascript event, which you are not sharing in the code, and I believe that there is the magic of the dynamism that you want to show, if you understand how to pass the values to a simple input you only need Pass it to a Select and update it.

var cod = document.getElementById("MUNICIPIOV").value;//Recibe el ID en la vista victima
document.datos.DEPARTAMENTO.value = cod;          
var combo = document.getElementById("MUNICIPIOV");
var valor = combo .options[combo .selectedIndex].text;//obtiene la descripcion            
$(".muniDenunciante").append('<option value="' + cod + '" selected>' + valor + '</option>').trigger("chosen:updated");//Agrega el valor, que supongo se carga vacio, puedes agregar el atributo 'selected' para hacer la comparación, utiliza **trigger("chosen:updated")** para actualizar el Select
    
answered by 19.02.2018 в 18:44
0

In short, what you want to do is a type of Select en Cascada .

<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </head>

  <body>
    <br>
    <br>
    <select name="country" id="country">
      <option value="">Seleccione</option>
    </select>
    <br>
    <br>
    <select name="teams" id="teams">
      <option value=""></option>
    </select>
  </body>
  
  <script type="text/javascript">
    var countries = ['Chile', 'España', 'Inglaterra', 'Italia', 'Alemania', 'Holanda', 'Argentina'];
    var option = '';
    
    for (var i = 0; i < countries.length; i++){
       option += '<option value="'+ countries[i] + '">' + countries[i] + '</option>';
    }
    $('#country').append(option);
    
    var teams = {
        'Chile': ['Colo-Colo el Más Grande', 'La U', 'Católica'],
        'España': ['Real Madrid', 'Barcelona', 'Atlético Madrid'],
        'Inglaterra': ['Manchester City', 'Chelsea', 'Manchester United'],
        'Italia': ['AC Milan', 'Juventus', 'Napoli'],
        'Alemania': ['Bayern Munich', 'Werder Bremen', 'Borussia Dortmund'],
        'Holanda': ['Ajax', 'PSV'],
        'Argentina': ['River Plate', 'Boca Juniors', 'Rosario Central']
    }
    
    var $teams = $('#teams');
    $('#country').change(function () {
        var country = $(this).val(), team = teams[country] || [];

        var html = $.map(team, function(t){
            return '<option value="' + t + '">' + t + '</option>'
        }).join('');
        $teams.html(html)
    });
  </script>
</html>

Tell us if it served you =)

    
answered by 19.02.2018 в 17:41