在JS输入字段中除以值并计算

时间:2015-12-04 11:40:19

标签: javascript calc

我是新手,我正在努力建立一个成本计算器。 在输入字段3上,我希望值除以80.因此,如果用户将80作为值,它将计数1.如果输入81,则它将计数2,依此类推。 你能帮忙吗?

这是我的代码:

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title></title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <script>
    function updatesum() {
    document.input.total.value = (document.input.input1.value -0) * (document.input.input2.value -0) * (document.input.input3value -0);
    return false;
        }
    </script>
    <body>      
<form name="input" action="#" method="get" onsubmit="return false;">
Calculator<br>
Value1:<input type="text" name="input1" value=" "><br>
Value2:<input type="text" name="input2" value="2"><br>
Value3:<input type="text" name="input3" value=" "><br>
Costs:<input type="text" name="total" value=" ">
<input type="submit" value="Submit" onclick="updatesum()">
</form> 

    </body>
</html>

非常感谢!

2 个答案:

答案 0 :(得分:0)

        <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title></title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <form name="input" action="#" method="get" onsubmit="return false;">
            Calculator<br>
            Value1:<input type="text" name="input1" value=""><br>
            Value2:<input type="text" name="input2" value="2"><br>
            Value3:<input type="text" name="input3" onblur="calcVal()" value=""><br>
            Costs:<input type="text" name="total" value="">
            <input type="button" value="Calculate" onclick="updatesum()">
        </form>
    <script type="text/javascript">
        function updatesum() {
            document.input.total.value = Math.ceil(document.input.input1.value / 80) * Math.ceil(document.input.input2.value / 80) * Math.ceil(document.input.input3.value / 80);
            return false;
        }

        function calcVal() {
           document.input.input3.value = Math.round(document.input.input3.value / 80) + 1;
        }
    </script>
    </body>
    </html>

答案 1 :(得分:0)

这是最终答案(经测试好)。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs fo">

<xsl:output method="xml"/>

<xsl:strip-space elements="*"/>

<xsl:template match="/images">
<new_images>
    <xsl:apply-templates/>
    </new_images>
</xsl:template>

<xsl:template match="item">
<xsl:for-each-group select="." group-by="@href">
    <xsl:copy-of select="current-group()[1]"/>
</xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>