带有2个参数的Javascript函数

带有2个参数的Javascript函数,javascript,Javascript,我需要创建一个包含两个参数(price1,price2)的函数,这些参数应该一起添加,并且在向它们添加Vat后,结果将显示在文本字段中 总的来说,我需要有3个文本字段:price1、price2和结果。此外,按下按钮计算后,应计算结果字段 到目前为止,我已经用document.write实现了一个返回函数,但它显然不显示任何文本字段,如下所示: function getTotalPrice(price1,price2,Vat) { var sum=price1+priceb; var V

我需要创建一个包含两个参数(
price1
price2
)的函数,这些参数应该一起添加,并且在向它们添加
Vat
后,结果将显示在文本字段中

总的来说,我需要有3个文本字段:price1price2结果。此外,按下按钮计算后,应计算结果字段

到目前为止,我已经用
document.write
实现了一个返回函数,但它显然不显示任何文本字段,如下所示:

function getTotalPrice(price1,price2,Vat)
{
  var sum=price1+priceb;
  var Vat=20;
  return ((sum*Vat)*100)/100;
}
document.write(getTotalPrice(4,3));
我不知道如何创建可以计算增值税的按钮,并将其显示在第三个文本框中。

问题:

  • Vat
    未用作参数,即使使用了它,它也会在代码中重新初始化,并指定一个值20
  • priceb
    是一个打字错误
  • 除此之外,代码似乎没有任何明显的问题

    function getTotalPrice(price1, price2, vat) {
      vat = (vat === undefined? 20 : vat); // give vat a default value if empty
      return (price1 + price2) * vat;
    }
    
    document.write(getTotalPrice(4, 3));
    
    编辑:根据下面的评论,这是真的,我想我应该继续,在这里简化数学。如果提问者心中有一个不同的等式,他可能应该多解释一点

    编辑: (vat===未定义?20:vat)正确,生成除未定义之外的任何值,默认值为20。 (增值税===未定义?增值税:20)只会产生未定义或20。

    问题:

  • Vat
    未用作参数,即使使用了它,它也会在代码中重新初始化,并指定一个值20
  • priceb
    是一个打字错误
  • 除此之外,代码似乎没有任何明显的问题

    function getTotalPrice(price1, price2, vat) {
      vat = (vat === undefined? 20 : vat); // give vat a default value if empty
      return (price1 + price2) * vat;
    }
    
    document.write(getTotalPrice(4, 3));
    
    编辑:根据下面的评论,这是真的,我想我应该继续,在这里简化数学。如果提问者心中有一个不同的等式,他可能应该多解释一点

    编辑: (vat===未定义?20:vat)正确,生成除未定义之外的任何值,默认值为20。
    (vat===未定义?vat:20)将仅产生未定义或20。

    按钮是用输入标记创建的。一个实现您想要的功能的示例:

    <input type="text" id="price1">
    <input type="text" id="price2">
    <input type="text" id="results">
    <input type="button" value="Calculate" onclick="calculateResults()">
    
    <script type="text/javascript">
    function calculateResults() {
        var price1Box = document.getElementById('price1');
        var price2Box = document.getElementById('price2');
        var resultsBox = document.getElementById('results');
        resultsBox.value = getTotalPrice(price1Box.value, price2Box.value);
    }
    </script>
    
    
    函数calculateResults(){
    var price1Box=document.getElementById('price1');
    var price2Box=document.getElementById('price2');
    var resultsBox=document.getElementById('results');
    resultsBox.value=getTotalPrice(price1Box.value,price2Box.value);
    }
    

    有更干净的方法可以做到这一点,但这是最实际和最简单的。如果希望
    getTotalPrice
    正常工作,您需要Bryan发布的修复程序。

    按钮是用输入标签创建的。一个实现您想要的功能的示例:

    <input type="text" id="price1">
    <input type="text" id="price2">
    <input type="text" id="results">
    <input type="button" value="Calculate" onclick="calculateResults()">
    
    <script type="text/javascript">
    function calculateResults() {
        var price1Box = document.getElementById('price1');
        var price2Box = document.getElementById('price2');
        var resultsBox = document.getElementById('results');
        resultsBox.value = getTotalPrice(price1Box.value, price2Box.value);
    }
    </script>
    
    
    函数calculateResults(){
    var price1Box=document.getElementById('price1');
    var price2Box=document.getElementById('price2');
    var resultsBox=document.getElementById('results');
    resultsBox.value=getTotalPrice(price1Box.value,price2Box.value);
    }
    

    有更干净的方法可以做到这一点,但这是最实际和最简单的。如果希望
    getTotalPrice
    正常工作,您需要Bryan发布的修复程序。

    这应该可以满足您的要求:

    <script type="text/javascript">
      function getTotalPrice(price1,price2)
      {
        var vat = 20;
        return (((price1+price2) * vat) * 100) / 100;
      }
    
      function calculate()
      {
        var result = document.getElementById('result');
        result.value = getTotalPrice(document.getElementById('price1').value, document.getElementById('price2').value);
      }
    </script>
    
    <form>
      <input type="text" id="price1" />
      <input type="text" id="price2" />
      <input type="text" id="result" />
      <input type="button" value="Calculate" onclick="calculate()" />
    </form>
    
    
    函数getTotalPrice(price1,price2)
    {
    增值税=20;
    退货(((价格1+价格2)*增值税)*100)/100;
    }
    函数计算()
    {
    var result=document.getElementById('result');
    result.value=getTotalPrice(document.getElementById('price1').value,document.getElementById('price2').value);
    }
    
    这应该满足您的要求:

    <script type="text/javascript">
      function getTotalPrice(price1,price2)
      {
        var vat = 20;
        return (((price1+price2) * vat) * 100) / 100;
      }
    
      function calculate()
      {
        var result = document.getElementById('result');
        result.value = getTotalPrice(document.getElementById('price1').value, document.getElementById('price2').value);
      }
    </script>
    
    <form>
      <input type="text" id="price1" />
      <input type="text" id="price2" />
      <input type="text" id="result" />
      <input type="button" value="Calculate" onclick="calculate()" />
    </form>
    
    
    函数getTotalPrice(price1,price2)
    {
    增值税=20;
    退货(((价格1+价格2)*增值税)*100)/100;
    }
    函数计算()
    {
    var result=document.getElementById('result');
    result.value=getTotalPrice(document.getElementById('price1').value,document.getElementById('price2').value);
    }
    
    为什么函数参数中有第三个(未使用的)
    Vat
    变量?感谢您指出,实际上,这个变量也不起作用。@Oded,可选参数在技术上很好。我的问题不仅仅是为什么它没有被使用,而是为什么
    Vat
    在函数中被重新声明,因为它将不再具有除20以外的任何值。为什么函数参数中有第三个(未使用的)
    Vat
    变量?感谢您指出,实际上,这个变量也不起作用。@Oded,可选参数在技术上很好。我的问题不仅仅是为什么它没有被使用,而是为什么
    Vat
    在函数中被重新声明,因为它将不会具有除20以外的任何值。除非我特别错了,否则似乎没有人注意到((sum*Vat)*100)/100==sum*Vat。我认为我的意图是(sum*((100+增值税)/100))@PAG,没错。我简化了上面的内容。除非我特别错了,否则似乎没有人注意到((总和*增值税)*100)/100==总和*增值税。我认为我的意图是(sum*((100+增值税)/100))@PAG,没错。我简化了上面的内容。谢谢,这正是我想要的。很抱歉,我已经写了一段时间了(你现在可能在笑),这些代码最终是我真正想做的最清晰的图片。再次非常感谢@不客气!如果你选择了这个答案,请接受。谢谢,这正是我想要的。很抱歉,我已经写了一段时间了(你现在可能在笑),这些代码最终是我真正想做的最清晰的图片。再次非常感谢@不客气!如果这是你选择的答案,请接受。