簡體   English   中英

如何從輸入類型編號中讀取特殊字符

[英]How to read special characters from input type number

在我的表單中,我有一些數字輸入字段。 通常,只允許一位數字。 輸入一位數字后,在onKeyUp上選擇下一個輸入字段。 但是對於需要多一位數字的情況,我想使用'*'作為指標。 因此,當用戶鍵入* 23時,它不會跳過。

我走了這么遠,但是當我得到輸入字段的值時,我收到一個空字符串。

我想從輸入中刪除星號。 當我嘗試在keydown事件(其中multi設置為true)中更改字段的輸入時,輸入字段的值不會更改。

更改輸入類型確實解決了值為"" ,但是該步驟不再起作用。

我覺得我正在為一堆我認為應該很簡單的代碼編寫大量代碼。

<input type="number" step="1" min="0" value="0">

var multi = 0;
// Keys to be ignored by the keyup event of .singleDigit.
var ignoreKeys = [ [8,46], [65,93], [107,222] ];
// Don't accept tab button, will skip an extra input field.
// Don't skip to next input, if input starts with *
// DOn't accept function buttons
$(".singleDigit").on('keyup', function(e){
  if(multi == 0
    && $.isNumeric($(this).val()) 
    && (e.which < ignoreKeys[0][0] || e.which > ignoreKeys[0][1])
    && (e.which < ignoreKeys[1][0] || e.which > ignoreKeys[1][1])
    && e.which < ignoreKeys[2][0]) {
    var inputs = $(this).closest('form').find('.input');
    inputs.eq( inputs.index(this) + 1).focus();
  }
});
// on focus out reset multi boolean.
$(".singleDigit").on('focusout', function() {
  multi = 0;
});
// check for key combination of *
var lastKey = null;
$(".singleDigit").on('keydown', function(e){
  if(lastKey != null) {
    if(lastKey == 16 && e.which == 56) {
      multi = 1;
    }
  }
  if(e.which != null) {
    lastKey = e.which;
  }
});

我找到了解決方法。 由於有了事件,因此可以阻止默認操作。

var lastKey = null;
$(".singleDigit").on('keydown', function(e){
  if(lastKey != null) {
    if(lastKey == 16 && e.which == 56) {
      multi = 1;
      e.preventDefault();
    } 
  }
  if(e.which != null) {
    lastKey = e.which;
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM