否则和if佣金

时间:2015-03-13 19:25:56

标签: javascript html onclick

鉴于这三个陈述:

  1. 如果贸易总额低于10,000美元,则为交易价值的0.1%
  2. 如果总交易价值大于或等于10,000美元,则为交易价值的0.08%
  3. 最低佣金为$ 5
  4. 如何计算委员会陈述?答案应在按钮“委员%”

    旁边的文本框中给出

2 个答案:

答案 0 :(得分:0)

根据您提供的信息,我认为您正在寻找类似的内容:

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

if(commission < 5)
    commission = 5.0;

然后只需将textBox的文本设置为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

答案 1 :(得分:0)

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

基于我的假设可能的答案:让我们假设您在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