Change values depending on the selected checkbox number

Asked

Viewed 86 times

0

I have several checkbox that pass the value to the input.

My question is this::

If I mark Monday, Tuesday, Wednesday, Thursday, input appear: Monday to Friday

If I mark the days Monday, Tuesday, Wednesday, Thursday, Saturday,Sunday input receive the value: Everyday

Man Jsfiddle.

  • You only have these two conditions?

  • would have these two more => from Monday to Saturday and another only one day, as if I had selected only EX: Thursday or Saturday etc

1 answer

1


You can do so according to your rules:

var inputs = $('input[name="dias[]"]');
inputs.on('change', function () {
    var str = [];
    var control = 0;
    inputs.each(function () {
        if (this.checked) {
        	str.push(this.value);
          control++;
        }
    });
    if(control == 7){
    	$('input[name="dias"]').val('Todos os Dias');
    }else if(str.join(',') == "Segunda,Terca,Quarta,Quinta,Sexta"){
    	$('input[name="dias"]').val('Segunda a Sexta');
    }else if(str.join(',') == "Segunda,Terca,Quarta,Quinta,Sexta,Sabado"){
    	$('input[name="dias"]').val('Segunda a Sábado');
    }else{
    	$('input[name="dias"]').val(str.join(','));
    }
    console.log($('input[name="dias"]').val());
});
#out{width:350px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" name="dias[]" value="Segunda">Segunda &nbsp;
<input type="checkbox" name="dias[]" value="Terca">Terça &nbsp;
<input type="checkbox" name="dias[]" value="Quarta">Quarta &nbsp;
<input type="checkbox" name="dias[]" value="Quinta">Quinta &nbsp;
<input type="checkbox" name="dias[]" value="Sexta">Sexta &nbsp;
<input type="checkbox" name="dias[]" value="Sabado">Sabado &nbsp;
<input type="checkbox" name="dias[]" value="Domingo">Domingo &nbsp;
<br>
<input type="text" name="dias" value="" id="out">

  • Perfect was that, I was even mounting something very similar in php to give a better idea. I will go deeper into javascript. Vlw

  • instead of putting control == 7 would be better str.length ==7?

  • The idea of control was even to control the other validations but as the rules are really specific with the length is more optimized.

Browser other questions tagged

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