How to make a table cell in the editable HTML table to accept numbers only (no letters)?

て烟熏妆下的殇ゞ 提交于 2020-01-06 07:34:23

问题


I am working with HTML5, JavaScript, JQuery.


回答1:


There's an $.isNumeric() function in jQuery 1.7, otherwise you'll have to write your own function in Javascript - there's a few answers on that already.




回答2:


I think your question is very similar to question but there might be a better answer. This following fiddle works for your case. I used the same code as in the mentioned question

$(document).ready(function() {
    $(".only-numbers").keydown(function(event) {
        // Allow: backspace, delete, tab, escape, and enter
         if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
              // Allow: Ctrl+A
             (event.keyCode == 65 && event.ctrlKey === true) || 
             // Allow: home, end, left, right
             (event.keyCode >= 35 && event.keyCode <= 39)) {
             // let it happen, don't do anything
             return;
        }
        else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            }   
    }
});

});



来源:https://stackoverflow.com/questions/12742738/how-to-make-a-table-cell-in-the-editable-html-table-to-accept-numbers-only-no-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!