jQuery UI Spinner-禁用键盘输入 [英] Jquery UI Spinner - Disable Keyboard Input

查看:231
本文介绍了jQuery UI Spinner-禁用键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种方法来阻止用户使用键盘通过jquery ui旋转器输入值.我有几个微调器,希望它只允许用户使用向上和向下箭头来增大或减小该值.

Im trying to find away to stop a user using the keyboard to input values via jquery ui spinners. I have several spinners and would like it to only allow the user to use the up and down arrows to increase or decrease the value.

我曾经尝试查看stackoverflow和google,但似乎找不到合适的方法.

Ive tried looking round stackoverflow and google and cant seem to find somethign that works.

例如,我有这样的输入:

So I have for example and input like this:

<input type="text" class="spinner" value="10"/>

并像这样初始化微调器:

and initialise the spinner like this:

$(".spinner").spinner();

,然后尝试像这样禁用按键:

and then try to disable keypress like this:

$(".spinner").unbind("keypress");

,它仍然允许键盘输入.有人知道这样做吗? 这是我已经测试过的一个JSBin文件: http://jsbin.com/ujajab/1/edit

and it still allows keyboard input. Does anyone know away to do this? Heres a JSBin file that Ive been testing with: http://jsbin.com/ujajab/1/edit

推荐答案

简单!替换

$(".spinner").unbind("keypress");

使用

$(".spinner").bind("keydown", function (event) {
    event.preventDefault();
});

Unbind只会解除绑定已绑定的功能.文本字段的默认行为不会更改;所以我们必须防止这种情况!如果您根本不希望它集中精力:

Unbind will just unbind the functions that have been binded. A textfields default behavior wont be changed; so we have to prevent that! If you don't want it to focus at all:

$(".spinner").focus(function () {
    $(this).blur();
});

祝您编程愉快!

这篇关于jQuery UI Spinner-禁用键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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