仅在可编辑列 td html 中接受数字 [英] Accept Number only in editable column td html

查看:17
本文介绍了仅在可编辑列 td html 中接受数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只在表格的可编辑列中验证编号有问题.所以这里是我的 html 代码:

I have problem with validate number only in editable column for my table. so here my html code:

<table>
<tr>
<td contenteditable='true' name="presentase"></td>
</tr>
</table>

这是我的 js 只用于数字:

and this my js for number only:

<script type="text/javascript">>
 $(".allow_only_numbers").keydown(function (e) {
            if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
              ((e.keyCode == 65 || e.keyCode == 86 || e.keyCode == 67) && (e.ctrlKey === true || e.metaKey === true)) ||
              (e.keyCode >= 35 && e.keyCode <= 40)) {
                return;
            }
            if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                e.preventDefault();
            }
        });
</script>

如何使该列只能在该td"标签上输入数字.我应该将.allow_only_numbers"放在列标签的什么位置?

how to make the column can input number only on that "td" tag. where should i place the ".allow_only_numbers" in the column tag ?

所以帮我解决这个问题.谢谢

so help me to solve this. thank you

推荐答案

你可以把 allow_only_numbers 类放到 td

你的 contenteditable 也需要 true

$(".allow_only_numbers").keydown(function (e) {
            if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
              ((e.keyCode == 65 || e.keyCode == 86 || e.keyCode == 67) && (e.ctrlKey === true || e.metaKey === true)) ||
              (e.keyCode >= 35 && e.keyCode <= 40)) {
                return;
            }
            if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                e.preventDefault();
            }
        });

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1" width="100px" height="50px">
<tr>
<td class="allow_only_numbers" contenteditable='true' name="presentase"></td>
</tr>
</table>

这篇关于仅在可编辑列 td html 中接受数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆