号码输入字段,禁止用户手动输入号码

发布于 2024-12-22 07:36:39 字数 292 浏览 3 评论 0原文

我有一个数字输入字段,用于设置事件的到期时间。即用户可以创建一个在 X 天后过期的事件; x 是使用输入字段中的数字来选择的。

我已将数字输入字段设置为最小值为 1,最大值为 7,但用户仍然可以手动输入他们想要的任何数字(即 0 或 > 8)。

我正在尝试找出一种方法来禁止用户手动在字段中输入数字,而不必使用 JQuery 等进行任何操作。

有谁知道这是否可能?

我正在开发的应用程序是在 Rails 中,但我认为这更多是一个 HTML/Javascript 问题。

预先非常感谢。

I have a number input field for setting the expiry of an event. i.e. a user can create an event that expires in X days; the x is chosen using a number in put field.

I have set the number input field to have a min value of 1 and max of 7, but a user can still manually type in whatever number they want (i.e. 0 or >8).

I am trying to figure out a way to disable the user from manually entering numbers into the field without having to do anything with JQuery, etc.

Does anyone know if this is possible?

The app I'm working on is in Rails, but I think this is more an HTML/Javascript question.

Thanks a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伊面 2024-12-29 07:36:39

这将阻止某人手动输入数字:

$('input').keypress(function(event){
    event.preventDefault();
});

示例: http://jsfiddle.net/ CWadc/10

This will prevent someone from manually entering a number:

$('input').keypress(function(event){
    event.preventDefault();
});

Example: http://jsfiddle.net/CWadc/10

热鲨 2024-12-29 07:36:39

您可以像这样限制有效数字(对于 jQuery 1.7.x);

$('#id_of_your_input').on('keydown', function(e) {
    if(e.keyCode != 46 && // delete
       e.keyCode != 8 && // backspace
       (e.keyCode < 49 || e.keyCode > 55) && //numbers above keyboard
       (e.keyCode < 97 || e.keyCode > 103) // numeric keyboard numbers
    ) {
        e.preventDefault();
    }
});

jsFiddle 示例

You can limit valid numbers like this (for jQuery 1.7.x);

$('#id_of_your_input').on('keydown', function(e) {
    if(e.keyCode != 46 && // delete
       e.keyCode != 8 && // backspace
       (e.keyCode < 49 || e.keyCode > 55) && //numbers above keyboard
       (e.keyCode < 97 || e.keyCode > 103) // numeric keyboard numbers
    ) {
        e.preventDefault();
    }
});

jsFiddle example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文