否则和 if 带有佣金的声明

else and if statements with commission

本文关键字:声明 if      更新时间:2023-09-26

给定以下三个语句:

  1. 如果总交易价值低于 10,000 美元,则为交易价值的 0.1%
  2. 如果总交易价值大于或等于 10,000 美元,则交易价值的 0.08%
  3. 最低佣金为5美元

我如何计算佣金报表?答案应在"佣金%"按钮旁边的文本框中给出

根据您提供的信息,我认为您正在寻找这样的东西:

var commission;
if(totalTradeValue < 10000)
    commission = tradeValue * .1;
else
    commission = tradeValue * .08;
if(commission < 5)
    commission = 5.0;

然后只需将文本框的文本设置为 commission .

编辑

<input type="text" id="totalTradeValue" placeholder="Total Trade Value" />
<input type="text" id="tradeValue" placeholder="This Trade Value" />
<input type ="button" value="Calculate" onclick="multiply()" />
<input type="text" id="total_commission" placeholder="Total Commission"/>

function multiply() {
    var commission;
    var totalTradeValue = document.getElementById('totalTradeValue').value;
    var tradeValue = document.getElementById('tradeValue').value;
    if(totalTradeValue < 10000)
        commission = tradeValue * .1;
    else
        commission = tradeValue * .08;
    if(commission < 5)
        commission = 5.0;
    document.getElementById('total_commission').value = commission;
}

有关工作示例,请参见 jsFiddle。

需要澄清:你想要一个文本框还是只是一个盒子?因为文本框是可编辑的。还 yu 是否希望在单击按钮之前禁用文本框?

基于我的假设的可能答案:假设您正在 html 表单变量 tradeVal 中获取交易价值,并且您可能有类似以下内容的代码:

<input type="text" name="tradeVal" />
<button name="commission" onclick="calculate(tradeVal)" />
<input type="text" name="commissionVal" id="commissionVal">

尝试使用 JS 函数,如下所示:

function calculate(tradeVal) {
      var commission = 5;
      if(tradeVal < 10000) {
            commission = tradeVal * .1;
      } else {
            commission = tradeVal * .08;
      }
      if(commission < 5) {
            commission = 5;
      }
      document.getElementById("commisssionVal").value = commission;
}

告诉我你是否想要别的东西。

编辑:

function multiply() { 
    var total = document.getElementById("total").value;
    var commission;
    if(total < 10000) {
        commission = total * 0.001;
    } else { 
        commission = total * 0.0008;
    }
    if(commission < 5) {    //set minimum commission to $5
        commission = 5;
    }
    var total_commission =commission+total; 
    document.getElementById("commisssion").value = total_commission; 
}

建议:我认为你对这个东西完全陌生。看看这个,从基础学习。