-1
In this script he calculates the total values of the checkbox marked, which adaptation would be necessary in addition to the sum he also show the attributes name
and value
of their checked checkboxes?
HTML:
<input type="checkbox" name="teste" checked="" value="20.00" />
<input type="checkbox" name="teste" checked="" value="20.00" />
<input type="checkbox" name="teste" checked="" value="20.00" />
<input type="checkbox" name="teste" checked="" value="20.00" />
<div id='resultado_soma'>
<?echo $total?>
</div>
<!--Resultado da soma dos
checkbox-->
<div id='resultado_soma_menos_variavel'>
<?echo $total_geral?>
</div>
<!--
Resultado Pegando
a Variavel - Resultado checkbox -->
Javascript:
(function() {
var elements = document.getElementsByTagName('input');
var resultado = document.getElementById('resultado_soma');
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener('change', calcular);
}
function calcular() {
var dados = [].map.call(elements, function(input) {
return {
el: input,
value: parseInt(input.value),
name: input.name
};
});
var total = dados.reduce(function(soma, el) {
return soma + (el.el.checked ? el.value : 0);
}, 0);
resultado.innerHTML = JSON.stringify(dados) + ' ' + total;
}
})();
These checkboxes don’t have
name
...– Sergio
sorry I didn’t put...
– Fabio Henrique
It is not quite clear what you need... take a look here: https://jsfiddle.net/s2uw6n57/ and try to clarify the question.
– Sergio
So if you click on the first box it will have to show me its name and value. and so on
– Fabio Henrique
Fabio, and show where? You still haven’t improved the question... to show respect to those who spend time helping is desirable that the question be clear, with code.
– Sergio
put an image above
– Fabio Henrique
It would be easier to put everyone inside a form and serialize... then just take the marked ones, and take the name and value
– Vinícius Lara
https://jsfiddle.net/s2uw6n57/1/ tá ai...
– Vinícius Lara
That’s right partner.... only that the calculation would have to come in a separate exit
– Fabio Henrique
Both are allocated into distinct variables
total
anddados
or you can put it on the way out as you wish...– Vinícius Lara
Amigo I am not aware of javascript como posso tratar essa saida
– Fabio Henrique