somatório de vários campos de input automaticamente
somatório de vários campos de input automaticamente
by Angelo Rogério Rubin
HTML
<input type="checkbox" name="um" id="um" value="10.00" checked>
<input name="subtotal[]" id="um" value="10.00" size="10" />
<div id="gas_">GAS</div>
<br/><br/>
JavaScript
var chk = document.getElementById('um');
var id;
var escolha = true;
function gastFun(checkbox) {
/*inicialmente o total é igual a 0*/
var total, i;
total = 0;
if (checkbox.checked) {
var price = checkbox.value;
var escolha = document.getElementById('gas_' + checkbox.id);
var prato = document.getElementById('show_details' + checkbox.id);
escolha.setAttribute('style', 'display:block;');
prato.setAttribute('style', 'display:block;');
/*função de incrementar dinamico dos buttoes*/
var valor = 1;
id('maisGAS_' + checkbox.id).onclick = function() {
valor += 1;
id('quantidadeGAS_' + checkbox.id).value = valor;
incre_d();
}
/** função de incrementar dinamico dos buttoes*/
id('menosGAS_' + checkbox.id).onclick = function() {
if (id('quantidadeGAS_' + checkbox.id).value > 1)
valor -= 1;
id('quantidadeGAS_' + checkbox.id).value = valor;
incre_d();
}
function incre_d() {
var quantidade = valor;
document.getElementById('quantiaGAS_' + checkbox.id).value = quantidade;
var subtotal = quantidade * price;
subtotal = subtotal.toFixed(2);
document.getElementById('subtotalGAS_' + checkbox.id).value = subtotal;
document.getElementById('show_item' + checkbox.id).innerHTML = subtotal + " EUR";
document.getElementById('qnt_' + checkbox.id).innerHTML = quantidade + "x";
/*iniciar aqui o sumatorio*/
};
var quantidade = valor;
document.getElementById('quantiaGAS_' + checkbox.id).value = quantidade;
var subtotal = quantidade * price;
subtotal = subtotal.toFixed(2);
document.getElementById('subtotalGAS_' + checkbox.id).value = subtotal;
document.getElementById('show_item' + checkbox.id).innerHTML = subtotal + "...