-4

我需要帮助来完成我的 php 项目。l 希望有一个文本字段 V1、V2、V3 和结果/输出只接受十进制数字(4 位小数,即 0.0000)或自动将数字转换为四位小数,并有一个只读字段生成总和3 个文本字段,无需重新加载表单。

例如 text field v1+text field v2+text field v3=text field result

文本字段应接受四位小数或自动将数字转换为四位小数。

我正在使用以下 sum 函数;

function sum() 
{
    var TotalLBCReceipts = num("TotalLBCReceipts");
    var TotalKaaseReceipts = num("TotalKaaseReceipts");
    var TakoradiToFactory = num("TakoradiToFactory2");
    var KaaseToFactory = num("KaaseToFactory2");
    var SampleResidue = num("SampleResidue");
    var Sweepings  = num("Sweepings");
    var OrganicCocoa = num("OrganicCocoa2");
    var Confiscated = num("Confiscated");
    var Cuttings = num("Cuttings");
    var r = document.getElementById("GRANDTOTAL1");

    if (r != null) 
    {
         r.value = TotalLBCReceipts + 
                   TotalKaaseReceipts + 
                   TakoradiToFactory +
                   KaaseToFactory +
                   SampleResidue +
                   Sweepings +
                   OrganicCocoa +
                   Confiscated+Cuttings;
     }
}

并使用获取值;

function num(id) 
{
    var e = document.getElementById(id);

    if (e != null) 
    {
        var v = e.value;
        if (/^\d+$/.test(v)) 
        {
            return parseInt(v, 10);
        }
    }

return 0;

}

谢谢。

4

1 回答 1

0

谷歌是你的朋友...... StackOverflow 也是如此!

您需要在这里使用 JavaScript,也许还有 PHP...

限制为带小数位的数字:

限制文本框的输入:只允许数字和小数点

<HTML>
  <HEAD>
    <SCRIPT language=Javascript>
       <!--
       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
       //-->
    </SCRIPT>
  </HEAD>
  <BODY>
    <INPUT id="txtChar" onkeypress="return isNumberKey(event)" 
           type="text" name="txtChar">
  </BODY>
</HTML>

对带有只读字段的数字求和:

添加文本框值并使用 javascript 显示它

<form name="myFormName">
    <p><input type="text" name="myInputName1" value="25.3"></p>
    <p><input type="text" name="myInputName2" value="14.2"></p>
</form>
<div id="total"></div>

<script type="text/javascript>

    var total = parseFloat(0, 10);

    total += parseFloat(document.myFormName.myInputName1.value, 10);
    total += parseFloat(document.myFormName.myInputName2.value, 10);

    document.getElementById("total").innerHTML = "Total is " + total;

</script>

四舍五入到小数点后 4 位;:

https://stackoverflow.com/a/7269919/1305169

Math.round(val*10000)/10000

希望这可以帮助!

编辑:

您需要将您的代码更改为以下内容;

你的 sum 功能:

function sum() 
{
    var TotalLBCReceipts = num("TotalLBCReceipts");
    var TotalKaaseReceipts = num("TotalKaaseReceipts");
    var TakoradiToFactory = num("TakoradiToFactory2");
    var KaaseToFactory = num("KaaseToFactory2");
    var SampleResidue = num("SampleResidue");
    var Sweepings  = num("Sweepings");
    var OrganicCocoa = num("OrganicCocoa2");
    var Confiscated = num("Confiscated");
    var Cuttings = num("Cuttings");
    var r = document.getElementById("GRANDTOTAL1");

    if (r != null) 
    {
         r.value = Math.round((
                       TotalLBCReceipts + 
                       TotalKaaseReceipts + 
                       TakoradiToFactory +
                       KaaseToFactory +
                       SampleResidue +
                       Sweepings +
                       OrganicCocoa +
                       Confiscated +
                       Cuttings) * 10000) / 10000;
     }
}

你的 num 功能:

function num(id) 
{
    var e = document.getElementById(id);

    if (e != null) 
    {
        return Math.round(e.value*10000)/10000;
    }

    return 0;
}

您可能只从num函数中返回纯数字,但是为了完整起见,我在这里展示它!

于 2013-02-08T14:25:29.663 回答