0

任何帮助将不胜感激。我正在使用带有小数的 onChange 命令添加 JavaScript 字段,但是当 onChange 命令移回选项“NO”以删除数字(例如 1.7)时,它只删除 1.69999 而不是 1.7。

下面是我正在使用的 onChange 代码。

<div id="onewindow">
   <label for="txtoneside" class="editor-label"><strong>1 Side </strong></label>
    <div align="right">
      <input type = "text" name = "txtoneside" id="txtoneside" value = "1.7" />
      <select name="txtoneside_slider" id="txtoneside_slider" data-role="slider" data-mini="true" onChange="javascript: if(this.value=='yes'){ document.getElementById('txtgrandtotal').value= parseFloat(document.getElementById('txtgrandtotal').value) + parseFloat(document.getElementById('txtoneside').value); calculate()}
if(this.value=='no'){ document.getElementById('txtgrandtotal').value= parseFloat(document.getElementById('txtgrandtotal').value) - parseFloat(document.getElementById('txtoneside').value); calculate()}"/>
         <option value="no">No</option>
         <option value="yes">Yes</option>
       </select> 
</div>
<div id="twowindow">
   <label for="txtoneside" class="editor-label"><strong>2 Side </strong></label>
<div align="right">
  <input type = "text" name = "txttwoside" id="txttwoside" value = "19.3" />
  <select name="txttwoside_slider" id="txttwoside_slider" data-role="slider" data-mini="true" onChange="javascript: if(this.value=='yes'){ document.getElementById('txtgrandtotal').value= parseFloat(document.getElementById('txtgrandtotal').value) + parseFloat(document.getElementById('txttwoside').value); calculate()}
if(this.value=='no'){ document.getElementById('txtgrandtotal').value= parseFloat(document.getElementById('txtgrandtotal').value) - parseFloat(document.getElementById('txttwoside').value); calculate()}" />
         <option value="no">No</option>
         <option value="yes">Yes</option>
       </select> 
</div>
<div id="grandtotal">
   <label for="txtgrandtotal" class="editor-label"><strong>JOB TOTAL</strong></label>
   <div align="right">
   <input type = "text" name = "txtgrandtotal" id="txtgrandtotal" value = "0" />
   </div>

</div>
4

1 回答 1

0

parseFloat() 正在转换为从二进制到十进制时绝不是精确转换的双精度。我建议根据您的需要使用 toFixed() 截断输出或查找库十进制数据类型。

看:

此外,在这种情况下,将一个值添加到总计然后将其减去是一种不好的做法,因为转换不精确 - 以这种方式会累积舍入误差。我建议在其中一个值发生变化时对每个包含的值进行总和。通过使用jQuery,您可以:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>New Web Project</title>
        <script src="jquery-1.9.1.min.js"></script>
    </head>
    <body>
        <div id="onewindow">
            <label for="txtoneside" class="editor-label"><strong>1 Side </strong></label>
            <div align="right">
                <input type="text" name="txtoneside" id="txtoneside" value = "1.7" />
                <select name="txtoneside_slider" id="txtoneside_slider" data-role="slider" data-mini="true" onChange="update()"/>
                    <option value="no">No</option>
                    <option value="yes">Yes</option>
                </select>
            </div>
            <div id="twowindow">
                <label for="txtoneside" class="editor-label"><strong>2 Side </strong></label>
                <div align="right">
            <input type = "text" name = "txttwoside" id="txttwoside" value = "19.3" />
                    <select name="txttwoside_slider" id="txttwoside_slider" data-role="slider" data-mini="true" onChange="update()"/>
                        <option value="no">No</option>
                        <option value="yes">Yes</option>
                    </select>
                </div>
                <div id="grandtotal">
                    <label for="txtgrandtotal" class="editor-label"><strong>JOB TOTAL</strong></label>
                    <div align="right">
                        <input type = "text" name = "txtgrandtotal" id="txtgrandtotal" value = "0" />
                    </div>
                </div>
            </div>
        </div>
        <script>
            update = function(){
                var all_values = [],
                    sum = 0,
                    i = 0;
                $('input:not(#txtgrandtotal)').each(function(){
                    var val = parseFloat($(this).attr('value'));
                    all_values.push((isNaN(val)) ? 0 : val) // store each value
                });
                $('select').each(function(){
                    var $option = $(this).find(':selected');
                    if($option.text()){
                        if($option.text()==="Yes")
                            sum+=all_values[i];  // sum only the ones that are selected
                    }
                    ++i;
                });
                $('#txtgrandtotal').attr('value', sum.toFixed(2));
                calculate(); // if that is required somewhere else in your code?
            }
        </script>
    </body>
</html>

编辑:当输入字段的值为“”或不是数字时,上面的代码已更正。在这种情况下,使用值 0 而不是 parseInt() 的 NaN 结果。

于 2013-03-23T23:50:27.073 回答