无法在JQuery中检查多个if else条件

我正在尝试在JQuery中检查某些多个条件,但未返回所需的结果。无论我输入什么,它总是进入其他条件。


$(function() {

  $('#installment').on("keydown keyup", check);

  function check() {

    var inst = Number($("#installment").val());

    if (inst === 2 || inst === 4 || inst === 6 || inst === 12) {

      return true;

    } else {

      $("#installment").val('');

      alert("you can not set wrong value");

      return false;

    }

  }

});

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

<tr>

  <td>Number of Installment</td>

  <td><input type="text" name="installment" id="installment"></td>

</tr>


慕的地8271018
浏览 189回答 3
3回答

慕标琳琳

$(function() {&nbsp; &nbsp; var val = $("#installment").val();&nbsp; $('#installment').on("keydown keyup",&nbsp; function () {&nbsp; &nbsp; var inst = Number($("#installment").val());&nbsp; &nbsp; if (inst === 2 || inst === 4 || inst === 6 || inst === 12) {&nbsp; &nbsp; &nbsp; alert("OK");&nbsp; &nbsp; &nbsp;return false;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; $("#installment").val('');&nbsp; &nbsp; &nbsp; alert("you can not set wrong value");&nbsp; &nbsp; &nbsp; //return false;&nbsp; &nbsp; }&nbsp; });});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript