我的if语句有什么问题

时间:2016-01-07 16:43:34

标签: jquery

我有一个简单的脚本来完成基本计算。我有一个动态填充的价格,但为了简单起见,我设定了一个不变的价格。采用预付定款的输入框设置了最小和最大属性。最小值是3000的10%,即300,最大值是3000.我的if语句一直告诉我,首付款总是小于或大于最小值,除非我使用最小值或最大值。在小提琴中,if语句适用于min和max,但不适用于我的实际站点。这是一个Fiddle供您检查。

HTML

<table>
  <tr id="payment">
    <td>Payment</td>
    <td>$3000</td>
  </tr>
  <tr id="down_payment">
    <td>Down Payment</td>
    <td>$300</td>
  </tr>
  <tr id="financed_amount">
    <td>Amount Financed</td>
    <td>$2700</td>
  </tr>
  <tr id="term">
    <td>Term</td>
    <td>22 months</td>
  </tr>
  <tr id="interest_rate">
    <td>Interest rate</td>
    <td>0%</td>
  </tr>
  <tr id="monthly_payment">
    <td>Monthly Payment</td>
    <td>$122.73</td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">
      <h5 style="font-size: .9em; text-align: center">Enter higher down payment to lower monthly payment</h5></td>
  </tr>
  <tr id="update_button">
    <td>
      <input type="number" value="" min="300" max="3000" name="change_amount" id="change_amount" style="width:100px; margin-left: 24%;" />
      <br>
      <label for="change_amount" class="error" style="display:none;margin-left: 10%;"></label>
    </td>
    <td>
      <input type="button" value="Update" name="update_down_payment" id="update_down_payment" />
    </td>
  </tr>
</table>

JS

$('#update_down_payment').click(function(e) {
  var down_payment = $('#change_amount').val();
  var min = $('#change_amount').attr('min');
  var max = $('#change_amount').attr('max');
  alert('down payment=' + down_payment + ' Min=' + min + ' Max=' + max);
  if (down_payment === "" || down_payment > max || down_payment < min) {
    $('.error').text("Please input down payment between " + Number(min).toLocaleString("en-US", {
      style: "currency",
      currency: "USD",
      minimumFractionDigits: 0
    }) + " and " + Number(max).toLocaleString("en-US", {
      style: "currency",
      currency: "USD",
      minimumFractionDigits: 0
    }));
    $('.error').show();
  } else {
    var payment = $("#payment td:nth-child(2)").text().replace(/\D/g, '') * 1;
    var financed_amount = (payment - down_payment);
    $('.error').hide();
    $('#down_payment td:nth-child(2)').text(Number(down_payment).toLocaleString("en-US", {
      style: "currency",
      currency: "USD",
      minimumFractionDigits: 0
    }));
    $('#financed_amount td:nth-child(2)').text(Number(financed_amount).toLocaleString("en-US", {
      style: "currency",
      currency: "USD",
      minimumFractionDigits: 0
    }));
    $('#monthly_payment td:nth-child(2)').text(Number(financed_amount / 22).toLocaleString("en-US", {
      style: "currency",
      currency: "USD",
      minimumFractionDigits: 0
    }));
  }
});

我的if语句

有问题

2 个答案:

答案 0 :(得分:3)

这将使其正常工作

  var min = parseInt($('#change_amount').attr('min'),10);
  var max = parseInt($('#change_amount').attr('max'),10);

答案 1 :(得分:2)

尝试更新的小提琴:

https://jsfiddle.net/t6rfkntb/8/

var min = Number($('#change_amount').attr('min'));
var max = Number($('#change_amount').attr('max'));`