将逗号分隔符添加到输入字段号将显示为NAN

时间:2018-11-12 11:25:09

标签: javascript jquery keyup

逗号已正确添加,但是我在控制台中收到此错误消息:

  

“指定的值“ NaN”不是有效数字”

我尝试将其设置为type="number",但仍是相同的警告。

jQuery(function() {
  var extra = 0;
  var $input = jQuery(".total_crm_records");

  $input.on("keyup", function(event) {

    // When user select text in the document, also abort.
    var selection = window.getSelection().toString();
    if (selection !== '') {
      return;
    }

    // When the arrow keys are pressed, abort.
    if ($.inArray(event.keyCode, [38, 40, 37, 39]) !== -1) {
      if (event.keyCode == 38) {
        extra = 1000;
      } else if (event.keyCode == 40) {
        extra = -1000;
      } else {
        return;
      }

    }

    var $this = jQuery(this);
    // Get the value.
    var input = $this.val();
    var input = input.replace(/[\D\s\._\-]+/g, "");
    input = input ? parseInt(input, 10) : 0;
    input += extra;
    extra = 0;
    $this.val(function() {
      return (input === 0) ? "" : input.toLocaleString("en-US");
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Input field : <input type="text" data-type="number" maxlength="20" class="total_crm_records">

0 个答案:

没有答案