문제

I'm working on a simple form calculation that sums whole numbers onclick.

The problem with the script which calculates money actually appends .00 at the end. I don't want decimal positions.

I am not good at JavaScript just wondering how to remove the ".00"

I just need a whole number no decimals.

My gut is that is has something to do with ['Set' + (i++)] but I'm not 100% sure on that.

<head>
<title>untitled</title>
<style type="text/css">

fieldset {
    float: left;
    width: 60px;
    padding: 5px;
    margin-right: 4px;
}
legend {
    font-weight: bold;
}
#total {
    font-size: 11px;
    width: 40px;
}

</style>
<script type="text/javascript">

function setRadios()
{
    function sumRadios()
    {
        var total = 0, i = 1, oForm = this.form;
        while (radgrp = oForm.elements['Set' + (i++)])
        {
            j = radgrp.length;
            do
                if (radgrp[--j].checked)
                {
                    total += Number(radgrp[j].value);
                    break;
                }
            while (j);
        }
        oForm.elements.total.value = total.toFixed(2);
    }

    var i = 0, input, inputs = document.getElementById('f1').getElementsByTagName('input');
    while (input = inputs.item(i++))
        if (input.name.match(/^Set\d+$/))
            input.onclick = sumRadios;
}

onload = setRadios;

</script>
</head>
<body>
<form id="f1">
<fieldset>

<legend>Set 1</legend>
<input id="r1" type="radio" name="Set1" value="5" /><label for="r1">5</label><br />
<input id="r2" type="radio" name="Set1" value="10" /><label for="r2">10</label><br />
<input id="r3" type="radio" name="Set1" value="15" /><label for="r3">15</label>
</fieldset>
<fieldset>

<legend>Set 2</legend>
<input id="r4" type="radio" name="Set2" value="10" /><label for="r4">10</label><br />
<input id="r5" type="radio" name="Set2" value="15" /><label for="r5">15</label><br />
<input id="r6" type="radio" name="Set2" value="25" /><label for="r6">25</label>
</fieldset>
<fieldset>

<legend>Set 3</legend>
<input id="r7" type="radio" name="Set3" value="1" /><label for="r7">1</label><br />
<input id="r8" type="radio" name="Set3" value="9" /><label for="r8">9</label><br />
<input id="r9" type="radio" name="Set3" value="24" /><label for="r9">24</label>
</fieldset>

<fieldset style="position:relative;top:36px;width:140px;">
<input id="total" type="text" name="total" value="" /><strong> total</strong>&nbsp;&nbsp;
<input type="reset" style="font-size:11px;" />
</fieldset>

</form>
</body>
</html>
도움이 되었습니까?

해결책

Replace:

total.toFixed(2);

With:

total.toFixed(0);

Or you could use parseInt

Or, since your inputs are all ints to start with, you can just leave out the toFixed out altogether.

See here for more on toFixed

다른 팁

Use the following code to avoid decimal places:

oForm.elements.total.value = total.toFixed(0);

You can see the usage in this tutorial:

http://www.w3schools.com/jsref/jsref_tofixed.asp

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top