问题
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