简体   繁体   中英

Javascript checkbox increment and decrement

What i want is that when a user click a checkbox in otherwords uncheck decrement when check it increment.

echo "<td><input type=checkbox name='check1' id='check1' value='".$info['eventCategory']."' onclick=if(this.checked){recal(" . $info['totalEvents'] . ",true)} else if(!this.checked){recal(" . $info['totalEvents'] . ",false)}   checked></td><td>" . $info['id'] . "      " . $info['name'] . "</td>";

i wrote above code and Only true statement is works

<script language="JavaScript">
            function recal(val,sum)
            { 
                if(sum == true)
                {
                    var total = parseInt(document.getElementById("total").innerHTML, 10);
                    total+=val;
                    document.getElementById("total").innerHTML=total;
                }
                else
                {
                    var total = parseInt(document.getElementById("total").innerHTML, 10);
                    total-=val;
                    document.getElementById("total").innerHTML=total;   
                }

            }
        </script>

What i want is to execute also else statement. I mean when user uncheck it must also decrease the number.

Thank you.

这对我有用:

echo <input type=checkbox name='check1' id='check1' value='".$info['eventCategory']."' onclick="recal(" . $info['totalEvents'] . ",this.checked)" checked>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM