如何从一个输入中拆分值然后计算它

时间:2018-03-25 08:59:45

标签: javascript jquery html math calculator

我有2个输入。 第一 - 输入,第二 - 输出。

最高200的用户数每个用户计算4美元,因此,如果我输入20个用户,我会在第二个输入中看到80个。

假设前200个用户每个用户支付4美元,而每个额外的1个是1美元,则计算从201到500的用户数。因此,如果我输入300个用户,它会计算200 x $ 4然后再加100 x $ 1,其中总计为900。

从501到1000的用户数为每位用户0.50美元。因此,如果我输入555个用户,它会计算200 x $ 4加300 x $ 1加55 x $ 0.50。总计是1127.50。

超过1000个用户的所有数字均以0.25美元的汇率计算。

$(document).ready(function() {
  function updatePrice() {
    var price = parseFloat($('#users-count').val());
    if (price <= '200') {
      var total = price * 4;
    } else if (price >= '201' && price <= '500') {
      var total = price * 1;
    } else if (price >= '501' && price <= '1000') {
      var total = price * 0.5;
    } else if (price >= '1000') {
      var total = price * 0.25;
    }
    var total = total.toFixed(0);
    $('#users-price').val(total);
  }
  $(document).on('change, keyup', '#users-count', updatePrice);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="calculator-price-field">
  <input type="number" id="users-count" class="calculator-field" value="1" min="1"><span class="calculator-field-label">users</span>
</div>
<div class="calculator-price-field">
  <input type="number" id="users-price" class="calculator-field" value="1" min="1"><span class="calculator-field-label">USD/month</span>
</div>

This is my codepen

感谢!!!

3 个答案:

答案 0 :(得分:2)

绑定多个事件的正确语法是在它们之间放置空格。

像这样:

&#13;
&#13;
$(document).ready(function() {
  function updatePrice() {
    var price = parseFloat($('#users-count').val());
    console.log(price);
    if (price <= '200') {
      var total = price * 4;
    } else if (price >= '201' && price <= '500') {
      var total = price * 1;
    } else if (price >= '501' && price <= '1000') {
      var total = price * 0.5;
    } else if (price >= '1000') {
      var total = price * 0.25;
    }
    var total = total.toFixed(0);
    $('#users-price').val(total);
  }
  $(document).on('change keyup', '#users-count', updatePrice);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="calculator-price-field">
  <input type="number" id="users-count" class="calculator-field" value="1" min="1"><span class="calculator-field-label">users</span>
</div>
<div class="calculator-price-field">
  <input type="number" id="users-price" class="calculator-field" value="1" min="1"><span class="calculator-field-label">USD/month</span>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

分别计算每个范围。此外,您需要检查末尾是否存在总数,以便在用户删除输入字段中的值时避免String

&#13;
&#13;
uri default_ringtone_uri = Settings.System.DeFAULT_RINGTONE_URI

Ringtone ringtone = RingtoneManager.getRingtone(context, uri).play();
&#13;
Uncaught TypeError
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以计算每个值,例如:

我还添加/更新了:

= .toFixed(2); - 这样你就可以得到小数点。您的预期值有小数点。

=添加条件,如果total不存在,请清除文本框

&#13;
&#13;
function updatePrice() {
  var price = parseFloat($('#users-count').val());

  if (price <= '200') {
    var total = price * 4;
  } else if (price >= '201' && price <= '500') {
    var total = (200 * 4) + ((price - 200));
  } else if (price >= '501' && price <= '1000') {
    var total = (200 * 4) + (300 * 1) + ((price - 500) * .5);
  } else if (price >= '1001') {
    var total = (200 * 4) + (300 * 1) + (500 * .5) + ((price - 1000) * .25);
  }
  var total = total.toFixed(2);

  if (total) $('#users-price').val(total);
  else $('#users-price').val("");
}
$(document).on('change, keyup', '#users-count', updatePrice);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="calculator-price-field">
  <input type="number" id="users-count" class="calculator-field" value="1" min="1"><span class="calculator-field-label">users</span>
</div>
<div class="calculator-price-field">
  <input type="number" id="users-price" class="calculator-field" value="1" min="1"><span class="calculator-field-label">USD/month</span>
</div>
&#13;
&#13;
&#13;