带有 2 个参数的 Javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6244938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:01:55  来源:igfitidea点击:

Javascript function with 2 arguments

javascript

提问by epsilon

I need to create a function with 2 arguments (price1, price2), which should be added together and the result would be displayed in a text field after adding Vatto them.

我需要创建一个带有 2 个参数 ( price1, price2)的函数,这些参数应该加在一起,结果将在添加后显示在文本字段中Vat

Overall, I need to have 3 text fields: price1, price2and results. In addition, the resultsfield should be calculated after pushing the button calculate.

总体来说,我需要有3个文本字段:价格1price2结果。此外,应在按下计算按钮后计算结果字段。

So far, I've come up with a return function with document.write, but it obviously doesn't display any text fields, which looks like that:

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

function getTotalPrice(price1,price2,Vat)
{
  var sum=price1+priceb;
  var Vat=20;
  return ((sum*Vat)*100)/100;
}
document.write(getTotalPrice(4,3));

I have no clue how to create the button that could calculate Vatand display it in the third text box.

我不知道如何创建可以计算Vat并将其显示在第三个文本框中的按钮。

回答by PAG

Buttons are created with the input tag. An example that does what you want:

按钮是用 input 标签创建的。一个可以满足您要求的示例:

<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>

There are cleaner ways to do this, but this is the most practical and simplest. You'll need the fixes posted by Bryan if you want getTotalPriceto work.

有更简洁的方法可以做到这一点,但这是最实用和最简单的方法。如果您想getTotalPrice工作,您将需要 Bryan 发布的修复程序。

回答by brymck

Issues:

问题:

  1. Vatis unused as an argument, and even if it were used it would always be reinitialized in your code and assigned a value of 20.
  2. pricebwas a typo.
  1. Vat未用作参数,即使使用它,它也将始终在您的代码中重新初始化并赋值为 20。
  2. priceb是一个错字。

Beyond that the code doesn't seem to have any evident problems.

除此之外,代码似乎没有任何明显的问题。

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));

Edit:Per the comment below, which is true, I figured I should just go ahead and simplify the math here. If the asker has a different equation in mind he should probably explain a bit more.

编辑:根据下面的评论,这是真的,我想我应该继续在这里简化数学。如果提问者有不同的等式,他可能应该多解释一点。

Edit:(vat === undefined? 20 : vat) is correct, producing any value other than undefined, default 20. (vat === undefined? vat : 20) will only produce undefinedor 20.

编辑:(vat === undefined? 20 : vat) 是正确的,产生除undefined以外的任何值,默认为 20。 (vat === undefined? vat : 20) 只会产生undefined20

回答by Ant

This should do what you're after:

这应该做你所追求的:

<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>