How to validate the following month by means of periodicity?

0

I have 3 selects the first select I choose the periodicity that I bring from a table can be bimonthly or biannual the second select I have the months also brought from a table and the third select same contains the months but it is only readonly then at the moment of eleji a periodicality example bimester and I choose the month of January I must fill the field month with the next month depending on the periodicity I am working with laravel javascript and mysql

(document).ready(function () {
    let $body = $("body"),
        $periodicidad_id = $("#periodicidad_id"),
        $fecha = $("#fecha"),
        $mes_inicio = $('#mes_inicio'),
        $mes_fin = $('#mes_fin'),
        $descripcion = $("#descripcion");

        $fecha.datepicker({
        autoclose: true,
        format: "yyyy",
        minViewMode: "years",
        startDate: "1972"
    })
    $periodicidad_id.on("change",function () {
        var periodo = $periodicidad_id.val();
        console.log($periodicidad_id.val());
        $.get('/catalogos/periodo-escolar/obtiene_periodo?periodo=' + periodo, function (data) {
            $.each(data, function (value) {
            })
        });
    })
    $mes_inicio.on("change",function () {
        var mes = $mes_inicio.val();
        $.get('/catalogos/periodo-escolar/obtiene_mes?mes=' + mes, function (data) {
            $.each(data, function (value) {
            })
        });
    })
       /* $mes_inicio.o
    
asked by marlon manuel 02.08.2018 в 19:59
source

1 answer

0

Actually you only have to bring the possible values of the database once (as well as the months). With that you define two variables in javascript that in my example are defined in hard.

The third select, even if readonly the same, will be clickable, but you can use CSS to give pointer-events: none .

var periodicidades = {
  'mensual': 1,
  'bimestral': 2,
  'trimestral': 3,
  'semestral': 6
};
var meses = ['Enero',
  'Febrero',
  'Marzo',
  'Abril',
  'Mayo',
  'Junio',
  'Julio',
  'Agosto',
  'Septiembre',
  'Octubre',
  'Noviembre',
  'Diciembre'
];

$(document).ready(function() {

  var $periodos = $('#periodos');
  var $meses = $('#meses');
  var $siguiente = $('#siguiente');

  Object.entries(periodicidades).forEach(function(periodicidad) {
    $periodos.append('<option value=' + periodicidad[1] + '>' + periodicidad[0] + '</option>');
  });
  meses.forEach(function(mes,index) {
    $meses.append('<option value=' + index + '>' + mes + '</option>');
    $siguiente.append('<option value=' + index + '>' + mes + '</option>');
  });

  function changeValores() {
    $mes = 1*$meses.val();
    $intervalo = 1 * $periodos.val();
    $siguiente.val(($mes + $intervalo) % 12);
  }
  $periodos.on('change', function() {
    changeValores();
  });
  $meses.on('change', function() {
    changeValores();
  });
  changeValores();


});
#siguiente {
  pointer-events:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  Elija período:
  <select id="periodos"></select>
</div>
<div>
  Elija mes
  <select id="meses"></select>
</div>
<div>
  El mes siguiente es
  <select type="text" id="siguiente"></select>
</div>
    
answered by 03.08.2018 в 15:09