【问题标题】:else and if statements with commission带佣金的 else 和 if 语句
【发布时间】:2015-03-13 19:25:56
【问题描述】:

鉴于这三个陈述:

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

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

【问题讨论】:

  • 您是否已经有了一些可以展示的代码?
  • @MaxwellS Here...
  • @Mary,请发布整个代码,包括带有按钮的部分(在您的帖子中缺少原始代码)。
  • var trade_value = (amount1*price1) + (amount2*price2) + (amount3*price3) if (trade_value
  • 我需要对此进行更改。现在,答案显示为提示,但现在我的任务是在单击相应按钮后将它们显示在一个框中。

标签: javascript html onclick


【解决方案1】:

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

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

有关工作示例,请参阅 the jsFiddle

【讨论】:

  • 我有:input type="button" value="Commission %" onclick="javascript:multiply()" />

【解决方案2】:

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

基于我的假设的可能答案:假设您以 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; 
}

建议:我认为您对这些东西完全陌生。看看this to learn from basics

【讨论】:

  • 它没有显示佣金。我知道如何获得它,但在警报提示中,但我现在的任务是要求将佣金显示在一个框中
  • function multiply() { if(total
  • 我觉得你做的有点不对。你说最低佣金是5美元。但是您为每个不应该发生的结果添加了 5 美元。相反,只有在计算后佣金低于 5 美元时,佣金才会为 5 美元。请参阅我编辑的答案。
  • 非常感谢。我看到你的回复晚了,但我能够自己弄清楚其余的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 2014-02-15
  • 2020-03-20
  • 2019-04-09
相关资源
最近更新 更多