Javascript 无法确定如何获得显示固定(2)金额的退货,无误

Javascript 无法确定如何获得显示固定(2)金额的退货,无误,javascript,html,Javascript,Html,该脚本非常适合在发票上添加项目,但是我无法确定如何使用.toFixed(2)将数据转换为显示$10.00而不是10。我每次尝试添加.toFixed(2)时都会出错。多谢各位 <script type="text/javascript"> function myFunction() { var answer = document.getElementById('total'); var x = document.getElementById('itemprice1')

该脚本非常适合在发票上添加项目,但是我无法确定如何使用.toFixed(2)将数据转换为显示$10.00而不是10。我每次尝试添加.toFixed(2)时都会出错。多谢各位

<script type="text/javascript">
function myFunction() {
    var answer = document.getElementById('total');

    var x = document.getElementById('itemprice1');
    var y = document.getElementById('itemprice2');
    var z = document.getElementById('itemprice3');
    var w = document.getElementById('itemprice4');
    var taxt = document.getElementById('taxtot');
    var thetot = document.getElementById('thetot');

    // parseFloat converts to values, otherwise you'll concatenate the strings.
    answer.value = parseFloat("0" + x.value) + parseFloat("0" + y.value) + parseFloat("0" + z.value) + parseFloat("0" + w.value);

}

function myFunction1() {
  var answer = document.getElementById('total');
    var taxt = document.getElementById('taxtot');
   var thetot = document.getElementById('thetot');

   thetot.value = parseFloat("0" + answer.value) + parseFloat("0" + taxt.value);
  if (thetot > "0") {


  {
    //function myFunction2() 
    var taxt = document.getElementById('taxtot');
    var tx1 = document.getElementById('tax1');
    var tx2 = document.getElementById('tax2');
     var tx3 = document.getElementById('tax3');
    var tx4 = document.getElementById('tax4');
    var x = document.getElementById('itemprice1');
    var y = document.getElementById('itemprice2');
    var z = document.getElementById('itemprice3');
    var w = document.getElementById('itemprice4');
    var answer = document.getElementById('total');



    taxt.value = parseFloat("0" + tx1.value) * ("0" + x.value) + parseFloat("0" + tx2.value) * ("0" + y.value) + parseFloat("0" + tx3.value) * ("0" + z.value) + parseFloat("0" + tx4.value) * ("0" + w.value);
  }
 }
  }
</script>

函数myFunction(){
var answer=document.getElementById('total');
var x=document.getElementById('itemprice1');
var y=document.getElementById('itemprice2');
var z=document.getElementById('itemprice3');
var w=document.getElementById('itemprice4');
var taxt=document.getElementById('taxtot');
var thetot=document.getElementById('thetot');
//parseFloat将转换为值,否则将连接字符串。
answer.value=parseFloat(“0”+x.value)+parseFloat(“0”+y.value)+parseFloat(“0”+z.value)+parseFloat(“0”+w.value”);
}
函数myFunction1(){
var answer=document.getElementById('total');
var taxt=document.getElementById('taxtot');
var thetot=document.getElementById('thetot');
thetot.value=parseFloat(0+answer.value)+parseFloat(0+taxt.value);
如果(按钮>“0”){
{
//函数myFunction2()
var taxt=document.getElementById('taxtot');
var tx1=document.getElementById('tax1');
var tx2=document.getElementById('tax2');
var tx3=document.getElementById('tax3');
var tx4=document.getElementById('tax4');
var x=document.getElementById('itemprice1');
var y=document.getElementById('itemprice2');
var z=document.getElementById('itemprice3');
var w=document.getElementById('itemprice4');
var answer=document.getElementById('total');
taxt.value=parseFloat(“0”+tx1.value)*(“0”+x.value)+parseFloat(“0”+tx2.value)*(“0”+y.value)+parseFloat(“0”+tx3.value)*(“0”+z.value)+parseFloat(“0”+tx4.value)*(“0”+w.value);
}
}
}

您的控件可能位于表单中,因此您只需在表单中使用它们的名称即可引用它们。您还可以使用一元
+
将字符串转换为数字:

function myFunction(formId) {
    var f = document.getElementById(formId);
    var answer =  f.total;
    var x = +f.itemprice1.value;
    var y = +f.itemprice2.value;
    var z = +f.itemprice3.value;
    var w = +f.itemprice4.value;
    var taxt = f.taxtot;
    var thetot = f.thetot;

    // Presumably here is where you want to use toFixed
    answer.value = '$' + (x + y + z + w).toFixed(2);
}

parseFloat(“0”+x.value)
这样的表达式可以是简单的
+x.value
,连接前导零是没有意义的。在哪里使用toFixed?您还应该去掉额外的}@robg,在等待输入时,需要零才能在页面上不显示错误
function myFunction(formId) {
    var f = document.getElementById(formId);
    var answer =  f.total;
    var x = +f.itemprice1.value;
    var y = +f.itemprice2.value;
    var z = +f.itemprice3.value;
    var w = +f.itemprice4.value;
    var taxt = f.taxtot;
    var thetot = f.thetot;

    // Presumably here is where you want to use toFixed
    answer.value = '$' + (x + y + z + w).toFixed(2);
}