Divide monetary value per checked checkbox

-1

I have a question about how to do this, checkbox or just one, enter a value in the "Total" input and show the result split, without refresh on the page, tried with .on change, but I can not get the checkded amount and value and show the split value.

Example.

$('input[type=checkbox]').on('change', function() {
  var total = $('input[type=checkbox]:checked').length;
  $('.resultado').html(total);
});


$('input').on('change', function() {
  $('.resultado2').html(this.value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype="checkbox" name="usuario[]" value="1">
<input type="checkbox" name="usuario[]" value="1">
<input type="checkbox" name="usuario[]" value="1">
<input type="checkbox" name="usuario[]" value="1">
<input type="text" value="0">
<div class="resultado2"></div>
<div class="resultado"></div>
    
asked by anonymous 01.03.2016 / 01:09

1 answer

2

Following is a simplification in the code:

function calcula() {
  var total = $('#total').val();
  var qtd = $('input[type=checkbox]:checked').length;
  $('#resultado').html( total / qtd );
}
$('input[type=checkbox]').on('change', calcula );
$('#total').on('input', calcula );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype="checkbox" name="usuario[]" value="1" checked>
<input type="checkbox" name="usuario[]" value="2" checked>
<input type="checkbox" name="usuario[]" value="3" checked>
<input type="checkbox" name="usuario[]" value="4" checked>
<input type="text" id="total" value="0">
<div id="resultado"></div>
    
01.03.2016 / 01:22