Change values depending on the number of checkboxes selected

0

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

My question is:

If I mark the days Monday, Tuesday, Wednesday, Thursday, Friday the input appears: Monday through Friday

If I mark the days Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday the input receives the value: Every Day

My JsFIddle .

    
asked by anonymous 12.07.2016 / 19:16

1 answer

1

You can do this 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><inputtype="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">
    
12.07.2016 / 19:44