Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript updatesum()问题_Javascript - Fatal编程技术网

javascript updatesum()问题

javascript updatesum()问题,javascript,Javascript,我有以下以HTML形式编写的JavaScript代码: <script type="text/javascript"> function updatesum() { document.functie.column8.value = (document.functie.column6.value -0) * (document.functie.column7.value -0); document.functie.column12.value = (docu

我有以下以HTML形式编写的JavaScript代码:

  <script type="text/javascript">
    function updatesum() {
    document.functie.column8.value = (document.functie.column6.value -0) * (document.functie.column7.value -0);
    document.functie.column12.value = (document.functie.column10.value -0) * (document.functie.column11.value -0);
    document.functie.column16.value = (document.functie.column14.value -0) * (document.functie.column15.value -0);
    document.functie.column20.value = (document.functie.column18.value -0) * (document.functie.column19.value -0);
   }

函数updatesum(){
document.functie.column8.value=(document.functie.column6.value-0)*(document.functie.column7.value-0);
document.functie.column12.value=(document.functie.column10.value-0)*(document.functie.column11.value-0);
document.functie.column16.value=(document.functie.column14.value-0)*(document.functie.column15.value-0);
document.functie.column20.value=(document.functie.column18.value-0)*(document.functie.column19.value-0);
}
使用php表单,如:

echo "<td><input name=\"column6\" onChange=\"updatesum()\"></td>";
echo "<td><input name=\"column7\" onChange=\"updatesum()\"></td>";
echo”“;
回声“;
  • 问题是,当我把一些值放在第一种形式时,它给了我和,但如果我不放一些值,它给了我零,我不想要:

  • 在底部,我有第三列中所有值的总和,但它给了我像2400或2300.44这样的数字,我想要输出2400或将2300.44四舍五入到2300。我该怎么做


  • 1/您与值“”相乘时自动转换为整数,因此0。以0结尾是正常的。如果您不希望这样做,则必须创建一个函数来检查输入的值,如果其中一个为“”,则返回“” 比如:

    function multiply(v1, v2) {
        if (v1 === '' || v2 === '' || isNaN(v1) || isNaN(v2)) {
            return '';
        } 
        return v1*v2;
    }
    
    2/您需要添加一个格式化函数,该函数将格式化您的代码,并在必须的位置添加
    ,并使用小数(或使用该注释中的链接):

    你要求举个例子。这将取代当前的
    updatesum
    功能:

    function multiply(v1, v2) {
        if (v1 === '' || v2 === '' || isNaN(v1) || isNaN(v2)) {
            return '';
        } 
        return v1*v2;
    }
    function multiplyAndFormat(v1, v2) {
        return formatNumber(multiply(v1, v2));
    }
    
    function formatNumber(num) {
        var formatted = '';
        num = Math.ceil(num) + '';
        for (var i = num.length - 1, j = 0; c = num.charAt(i); i--) {
            if (j % 3 === 0) {
                c = c + ',';
            }
            formatted = c + formatted;
            j++;
        }
        return formatted.trim();
    }
    
    function updatesum() {
        document.functie.column8.value = multiplyAndFormat(document.functie.column6.value, document.functie.column7.value);
        document.functie.column12.value = multiplyAndFormat(document.functie.column10.value, document.functie.column11.value);
        document.functie.column16.value = multiplyAndFormat(document.functie.column14.value, document.functie.column15.value);
        document.functie.column20.value = multiplyAndFormat(document.functie.column18.value, document.functie.column19.value);
    }
    

    应该这样做

    1/您正在与值“”相乘,该值将自动转换为整数,因此0。以0结尾是正常的。如果您不希望这样做,则必须创建一个函数来检查输入的值,如果其中一个为“”,则返回“” 比如:

    function multiply(v1, v2) {
        if (v1 === '' || v2 === '' || isNaN(v1) || isNaN(v2)) {
            return '';
        } 
        return v1*v2;
    }
    
    2/您需要添加一个格式化函数,该函数将格式化您的代码,并在必须的位置添加
    ,并使用小数(或使用该注释中的链接):

    你要求举个例子。这将取代当前的
    updatesum
    功能:

    function multiply(v1, v2) {
        if (v1 === '' || v2 === '' || isNaN(v1) || isNaN(v2)) {
            return '';
        } 
        return v1*v2;
    }
    function multiplyAndFormat(v1, v2) {
        return formatNumber(multiply(v1, v2));
    }
    
    function formatNumber(num) {
        var formatted = '';
        num = Math.ceil(num) + '';
        for (var i = num.length - 1, j = 0; c = num.charAt(i); i--) {
            if (j % 3 === 0) {
                c = c + ',';
            }
            formatted = c + formatted;
            j++;
        }
        return formatted.trim();
    }
    
    function updatesum() {
        document.functie.column8.value = multiplyAndFormat(document.functie.column6.value, document.functie.column7.value);
        document.functie.column12.value = multiplyAndFormat(document.functie.column10.value, document.functie.column11.value);
        document.functie.column16.value = multiplyAndFormat(document.functie.column14.value, document.functie.column15.value);
        document.functie.column20.value = multiplyAndFormat(document.functie.column18.value, document.functie.column19.value);
    }
    

    应该这样做

    你应该只问一个问题。“它给我零,我不想要”你想要什么?“我希望输出值为2400或将2300.44四舍五入到2300”参见和。“如果我不输入一些值,它会给我零,这是我不想要的”…那么你想要什么?抱歉,Felix Kling和Shawn31313是两个问题。。。问题一:我不想看到任何字符,因为该字符(在我的例子中为零)是在mysql数据库中提交的。您应该只问一个问题。“它给我零,我不想要”你想要什么?“我希望输出值为2400或将2300.44四舍五入到2300”参见和。“如果我不输入一些值,它会给我零,这是我不想要的”…那么你想要什么?抱歉,Felix Kling和Shawn31313是两个问题。。。在问题一:我不希望看到任何字符,因为该字符(在我的例子中为零)在mysql数据库中提交。dievardump:您能提供一个在我的函数中使用我的代码的示例吗?。我尝试过,但没有成功:(.问题是,在php字段中我只能有一个函数,在javascript中我必须检查类似于php的document.functie.column“number.value”,所以您的示例可能是功能性的,但对于javascript的新手,我不知道如何修改它来工作。谢谢。现在在php代码中我必须有multiplyAndFormat(),不再需要更新了?我再次更新了我的帖子。我想说的是,如果你不能理解代码以及如何处理它,你应该停止进一步的研究,回到基础上来。我们不是来编写代码的,而是来帮助你的。很抱歉,我不会写更多关于这个主题的代码,除非在必要时纠正错误这是你的代码。它可以工作99%。实际上,如果(j%3==0),它总是给我一个类似“0”或“455”的格式,则更改为(j%3==1),它可以工作到数千,比如1000或34000,但如果我得到一些小数,它会给我1000,00,这是不正确的,它一定是1000.00…迪瓦德姆普:对不起,但我承认我很难理解javascript,在我的项目中,我只需要做一些实时数学。j!==0&&j%3==0应该这样做。迪瓦德姆普:可以吗请提供一个在我的函数?上使用我的代码的示例。我已经尝试过,但没有成功:(。问题是,我在php字段中只能有一个函数,在javascript中,我必须检查类似php的document.functie.column“number”.value,所以您的示例可能是功能性的,但对于javascript新手,我不知道如何修改它才能工作。谢谢。现在在php代码中,我必须有multiplyAndFormat(),不再需要更新了?我再次更新了我的帖子。我想说的是,如果你不能理解代码以及如何处理它,你应该停止进一步的研究,回到基础上来。我们不是来编写代码的,而是来帮助你的。很抱歉,我不会写更多关于这个主题的代码,除非在必要时纠正错误这是你的代码。它可以工作99%。实际上,如果(j%3==0),它总是给我一个类似“0”或“455”的格式,则更改为(j%3==1),它可以工作到数千,比如1000或34000,但如果我得到一些小数,它会给我1000,00,这是不正确的,它一定是1000.00…迪瓦杜姆:对不起,但我承认我很难理解javascript,在我的项目中,我只需要做一些实时数学。j!==0&&j%3==0应该做。