2017-10-11 60 views
0

我用這個angularjs指令來限制用戶只按數字。而且絕對有效。但是,當我使用刪除鍵來刪除輸入字段數據。它不適用於我。因此,任何人都有一個想法,允許刪除鍵來刪除輸入字段數據。AngularJs指令:如何使用刪除鍵刪除輸入字段數據

請幫我搞定這個。任何幫助將不勝感激。

在此先感謝。

app.directive('allowOnlyNumbers', function() { 
return { 
    restrict: 'A', 
    link: function (scope, elm, attrs, ctrl) { 
     elm.on('keydown', function (event) { 
      if (event.which == 64 || event.which == 16) { 
       // to allow numbers 
       return false; 
      } else if (event.which >= 48 && event.which <= 57) { 
       // to allow numbers 
       return true; 
      } else if (event.which >= 96 && event.which <= 105) { 
       // to allow numpad number 
       return true; 
      } else if ([8, 13, 27, 37, 38, 39, 40].indexOf(event.which) > -1) { 
       // to allow backspace, enter, escape, arrows 
       return true; 
      } else { 
       event.preventDefault(); 
       // to stop others 
       return false; 
      } 
     }); 
    } 
} 

});

+1

刪除鍵的鍵碼是'46' – pbibergal

回答

0

如果將其中一部分添加到您的指令中,請添加此部分。

else if (event.which == 46) { 
    // to allow delete 
    return true; 
} 

希望它能工作。