How to serialize a checkbox form

Asked

Viewed 122 times

-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?

inserir a descrição da imagem aqui

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...

  • sorry I didn’t put...

  • It is not quite clear what you need... take a look here: https://jsfiddle.net/s2uw6n57/ and try to clarify the question.

  • So if you click on the first box it will have to show me its name and value. and so on

  • 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.

  • put an image above

  • It would be easier to put everyone inside a form and serialize... then just take the marked ones, and take the name and value

  • https://jsfiddle.net/s2uw6n57/1/ tá ai...

  • That’s right partner.... only that the calculation would have to come in a separate exit

  • Both are allocated into distinct variables total and dados or you can put it on the way out as you wish...

  • Amigo I am not aware of javascript como posso tratar essa saida

Show 6 more comments

1 answer

2


I don’t know if I got it right, but take a look at this:

First thing, give a DIFFERENT name for each checkbox: teste1, teste2, teste3 etc...

<input type="checkbox" name="teste1" value="20.00" />
<input type="checkbox" name="teste2" value="20.00" />
<input type="checkbox" name="teste3" value="20.00" />
<input type="checkbox" name="teste4" value="20.00" />

Then where is:

resultado.innerHTML = JSON.stringify(dados) + ' ' + total;

Trade in for:

//resultado.innerHTML = JSON.stringify(dados) + ' ' + total;
   resultado.innerHTML = $("#form").serialize().replace(/&/g, '<br />') + '<br /><br />R$ ' + total.toFixed(2).replace(".",",");

See that I commented the first line (the one I had before) because I don’t know what you want to do with it.

Try it on this Fiddle: https://jsfiddle.net/s2uw6n57/3/

  • Exactly that David , just need the sum to appear in a separate div

  • @Fabiohenrique Just create one more div <div id="resultado"></div> and trade resultado.innerHTML for document.getElementById("resultado").innerHTML.

  • I tried here but still coming all in the same div put here an image as wanted http://i.imgur.com/1kqQILb.jpg

  • Excuse my ignorance but I don’t know anything about javascript

  • @Fabiohenrique I got it. Take a look at this Fiddle I updated: https://jsfiddle.net/s2uw6n57/5/

  • Perfect now... Thank you

Show 1 more comment

Browser other questions tagged

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