JS output sum (only the numbers) of all checked input fields values before form submit

CiprianD

I have a form that has input checkboxes like the ones in the example. The js code extracts the number from the checked input values and outputs them one next to eachother. How to I edit the code so that when you check a second input checkbox the output is the sum of the numbers?

This is the PHP form:

<input type="checkbox" onchange="toggleCheckbox(this)" value="abc 1 def" name="1">
<input type="checkbox" onchange="toggleCheckbox(this)" value="abc 2 def" name="2">
<input type="checkbox" onchange="toggleCheckbox(this)" value="abc 3 def" name="3">

The JavaScript i use:

<script type="text/javascript"> 
    function toggleCheckbox(element){
        if (element.checked){
        var number = element.value.replace ( /[^\d.]/g, '' );
        document.getElementById("test").innerHTML += " " + number;
        }
} 
</script>

The HTML field:

<p id="test"></p>
lpg

You could store the accomulated value in a global variable, E.g.:

var sum = 0;

function toggleCheckbox(element){
        var number = element.value.replace ( /[^\d.]/g, '' );
        if (element.checked)
            sum += Number(number);
        else
            sum -= Number(number);
        document.getElementById("test").innerHTML = sum;
} 

Demo fiddle : http://jsfiddle.net/lparcerisa/08hLc37a/

With jQuery, it could be done this way:

$("input[type=checkbox]").click(function(){
    var sum=0;
    $("input[type=checkbox]:checked").each(function(index){
        sum += Number($(this).val().replace( /[^\d.]/g,''));
    });
    $("#test").html(sum);
} );

Demo fiddle (jquery) : http://jsfiddle.net/lparcerisa/res4k96j/2/

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Subtract values only if all are numbers

분류에서Dev

How to reset form fields in angular js after form submit

분류에서Dev

Submit form when a radiobox is checked

분류에서Dev

How to Generate a List of all Input name="" fields in a form with Javascript/JQuery

분류에서Dev

Allow only numbers and multiple dots in jquery form input

분류에서Dev

input and sum the numbers - java

분류에서Dev

Ignore alphabets in a cell, only numbers should be checked

분류에서Dev

mongodb $ sum only postive Numbers

분류에서Dev

how to apply js for all input type which are in form

분류에서Dev

Sum up input fields after some calulations

분류에서Dev

Modifications to loop to make sure all boxes are checked before proceeding

분류에서Dev

Form validate combine input fields at once codeigniter

분류에서Dev

Any way to force a form to submit fields differently without using Javascript?

분류에서Dev

How to submit two forms at a time and store first form submit output value as 2nd form value?

분류에서Dev

Changing WTForm fields depending on input into other fields in the form

분류에서Dev

If a checkbox is checked, find how many user input values are the same

분류에서Dev

Hide submit button for PayPal payment form if input field is not 40 characters

분류에서Dev

php submit only giving last value from array that populates form

분류에서Dev

"For" loop takes all values before AsyncTask is executing

분류에서Dev

How to hide all input elements within a form?

분류에서Dev

Knockout js checkbox is not checked when setting selected values in the view model

분류에서Dev

No output over SSH before waiting for input via scanf()

분류에서Dev

Regex - find all kinds of numbers in a String before a specific Character

분류에서Dev

How to disable/enable input fields of a form dynamically on Django

분류에서Dev

Get all the possible combinations of the given numbers to reach at a given sum

분류에서Dev

Pass Form Values to Update Query without updating blank fields in table

분류에서Dev

How do I only allow a user to submit an html form only once?

분류에서Dev

How to get values of dynamically created input fields (Json)

분류에서Dev

getElementsByName looping through all fields returning values using onChange event

Related 관련 기사

  1. 1

    Subtract values only if all are numbers

  2. 2

    How to reset form fields in angular js after form submit

  3. 3

    Submit form when a radiobox is checked

  4. 4

    How to Generate a List of all Input name="" fields in a form with Javascript/JQuery

  5. 5

    Allow only numbers and multiple dots in jquery form input

  6. 6

    input and sum the numbers - java

  7. 7

    Ignore alphabets in a cell, only numbers should be checked

  8. 8

    mongodb $ sum only postive Numbers

  9. 9

    how to apply js for all input type which are in form

  10. 10

    Sum up input fields after some calulations

  11. 11

    Modifications to loop to make sure all boxes are checked before proceeding

  12. 12

    Form validate combine input fields at once codeigniter

  13. 13

    Any way to force a form to submit fields differently without using Javascript?

  14. 14

    How to submit two forms at a time and store first form submit output value as 2nd form value?

  15. 15

    Changing WTForm fields depending on input into other fields in the form

  16. 16

    If a checkbox is checked, find how many user input values are the same

  17. 17

    Hide submit button for PayPal payment form if input field is not 40 characters

  18. 18

    php submit only giving last value from array that populates form

  19. 19

    "For" loop takes all values before AsyncTask is executing

  20. 20

    How to hide all input elements within a form?

  21. 21

    Knockout js checkbox is not checked when setting selected values in the view model

  22. 22

    No output over SSH before waiting for input via scanf()

  23. 23

    Regex - find all kinds of numbers in a String before a specific Character

  24. 24

    How to disable/enable input fields of a form dynamically on Django

  25. 25

    Get all the possible combinations of the given numbers to reach at a given sum

  26. 26

    Pass Form Values to Update Query without updating blank fields in table

  27. 27

    How do I only allow a user to submit an html form only once?

  28. 28

    How to get values of dynamically created input fields (Json)

  29. 29

    getElementsByName looping through all fields returning values using onChange event

뜨겁다태그

보관