Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.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 如何编写函数来计算总价,清除按钮值并提交表单_Javascript_Html - Fatal编程技术网

Javascript 如何编写函数来计算总价,清除按钮值并提交表单

Javascript 如何编写函数来计算总价,清除按钮值并提交表单,javascript,html,Javascript,Html,我已经尝试了所有方法,但似乎都不起作用,而且我对javascript还不熟悉,如果有任何帮助,我将不胜感激。 多谢各位 项目说明 量 价格 总价 函数calculateTotal(){var totalPrice=“$450”;var Quantity=document.getElementById(“Quantity”).value;var price=document.getElementById(“price”).value;total=Quantity*price;alert(“数量

我已经尝试了所有方法,但似乎都不起作用,而且我对javascript还不熟悉,如果有任何帮助,我将不胜感激。 多谢各位


项目说明
量
价格
总价
函数calculateTotal(){var totalPrice=“$450”;var Quantity=document.getElementById(“Quantity”).value;var price=document.getElementById(“price”).value;total=Quantity*price;alert(“数量不能为零、空白或空”);alert(“价格不能为零、空白或空”);document.getElementById(“totalPrice”).innerHTML=totalPrice;return;}函数clearTotal(){document.getElementById(“数量”).innerHTML=“”;document.getElementById(“价格”).innerHTML=“”;return;}函数
submit(){return confirm(“信息正确吗?\n itemsdescription=RedBowl-nQuantity=1 nPrice=450”);}
函数calculateTotal(){
var totalPrice=“$450”;

//在“total=quantity*price中,case很重要,因此……您的代码存在无数问题,但最重要的是可读性。请学习如何构造代码

需要指出的错误很少:

  • 没有“提交时”方法
  • 读取JS函数的命名约定,因为“calculate total()”不是一个有效的名称,您在calculate total按钮上使用该名称
  • 您试图通过ID获取元素,但忘记分配任何元素
  • 同样,命名约定“数量”用于存储数量输入框的值,但您在计算中使用了“数量”
  • 不能为输入框设置innerHtml
  • 您为“totalPrice”指定了一个字符串值,然后在乘法中使用相同的值
  • 
    项目说明
    量
    价格
    总价
    函数calculateTotal(){
    //const totalPrice=“450”;
    让数量=document.getElementById(“数量”).value;
    让price=document.getElementById(“price”).value;
    总数=数量*价格;
    console.log(总计)
    document.getElementById(“totalPrice”).value=total;
    }
    函数clearTotal(){
    document.getElementById(“数量”).value=1;
    document.getElementById(“totalPrice”).value=450;
    } 
    函数submit(){
    返回窗口。确认(“信息正确吗?\n itemsdescription=RedBowl-nQuantity=1 nPrice=450美元”);
    }
    
    total=quantity*price;
    此处未声明
    total
    (使其成为
    var total
    ),且
    数量
    数量
    ,您可以返回
    总计
    。请查看简单示例。。。
    function calculateTotal() { 
        var totalPrice = "$450"; 
        
        //case matters as in "total = quantity * price <- quantity is lowercase
        //var Quantity = document.getElementById("quantity").value; 
        var quantity = document.getElementById("quantity").value; 
        
        var price = document.getElementById("price").value; 
        total = quantity * price; 
        alert("Quantity cannot be zero, blank or null"); 
        alert("Price cannot be zero, blank or null"); 
    
        //do you want the value from "total = quantity * price"? or the "var totalPrice = "$450""?
        document.getElementById("totalPrice").innerHTML = totalPrice; <- you probably want "total" not "total price"
        return; 
    } 
    
    function clearTotal() 
    { 
        document.getElementById("Quantity").innerHTML = ""; 
        document.getElementById("price").innerHTML = ""; 
        return; 
    } 
    
    function submit() 
    { 
        return confirm( "is the information correct ? \n ItemDescription = RedBowl nQuantity = 1 nPrice = $450" ); 
    }