Generate options from a select (years) according to another selected field

Asked

Viewed 694 times

0

I have a registration form for dependents of a plan, where you can choose son, husband, father-in-law, etc. and then the year of birth

He wanted, when selecting what the degree of kinship, he go changing the options of select, ex:

Selecionar filho: Máximo ano atual | Mínimo - 35 anos (1982 a 2017)
Pai/mãe:  Máximo ano atual - 12 | Mínimo ano atual - 100 (1917 a 2005)
Outros: Máximo ano atual | Mínimo ano atual - 100 (1917 a 2017)

Can use php, javascript, jquery, qq a.

I was only able to create a rule, I don’t know how to attach to the other select:

<select id="grau_parentesco">
<option value="filho">Filho(a)</option>
<option value="pai">Pai</option>
<option value="mae">Mãe</option>
<option value="outros">Outros</option>
</select>
<select id="ano"></option>
<?
$ano_inicial = date("Y");
$qtd = 35;
for($i=0; $i <= $qtd; $i++){
    $ano = $ano_inicial-$i;
    $ano = sprintf("%02s",$ano);
    echo "<option value=\"".$ano."\">".$ano."</option>\n";
}
?>
</select>
  • You mean thus?

  • Or this: https://answall.com/a/14668/43731

  • exactly, but the difference is that I need you to run the years automatically, so that no matter what year we’re in, he does it every year, so that I don’t have to change the code every year

1 answer

2


Follow an example used Jquery and a simple logic, I left the rules on a switch to facilitate if I wanted to add other.

$("#grau_parentesco").on('change', function () {

        //Selecionar 
        //filho: Máximo ano atual | Mínimo - 35 anos (1982 a 2017)
        //Pai / mãe:  Máximo ano atual - 12 | Mínimo ano atual - 100(1917 a 2005)
        //Outros: Máximo ano atual | Mínimo ano atual - 100(1917 a 2017)


        var maxValue = (new Date()).getFullYear();
        var minValue = maxValue - 100;

        switch ($(this).val()) {
            case "pai":
            case "mae":
                maxValue -= 12;
                break;
            case "filho":
                minValue = maxValue - 35;
                break;
            default:
                break;
        }
        
        //limpa
        $("#ano").find("option").remove();

        //popula
        for (var ano = minValue; ano <= maxValue; ano++) {
           $('#ano').append($('<option>', {
                value: ano,
                text: ano
            }));
        }

    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="grau_parentesco">
  <option value="">Selecione</option>
  <option value="filho">Filho(a)</option>
  <option value="pai">Pai</option>
  <option value="mae">Mãe</option>
  <option value="outros">Outros</option>
</select>

<select id="ano">
</select>

  • perfect, that’s right. just another frecurite.. rs, how do I appear the years reversed, the first option from above the child for example be 2017

  • just reverse the population, (var year = maxValue; year >= minValue ; year--)

  • Perfect, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.