使用jquery启用和禁用键盘输入 [英] Enabling and disabling keyboard input using jquery
问题描述
我有一个表单,我想禁用键盘输入。但是我仍然希望将表单作为集合的一部分进行回发,因此添加禁用属性对我来说不是解决方案。
I have a form where I want to disable keyboard input. However I still want to postback the form as part of collection so adding the 'disabled' attribute isnt the solution for me.
我已添加此代码以使输入只读:
I have added this code to make the input readonly:
//code to not allow any changes to be made to input field
$(".customreadonly").keydown(function () {
return false;
});
然后我像这样设置这个类:
And then I set this class like this:
$("#StartRRP").addClass('customreadonly');
但是当我删除这样的类时:
However when I remove the class like this:
$("#StartRRP").removeClass('customreadonly');
输入仍然不允许键盘输入。这似乎是微不足道的,我做错了什么?
The input still wont allow keyboard input. This seems to be trivial, what am I doing wrong ?
提前致谢...
推荐答案
当你将事件直接附加到jQuery对象时,它甚至在它的初始选择器不再有效(即删除了类)之后仍然存在。
When you attach the event directly to the jQuery object, it remains even after the initial selector to it is no longer valid(ie, class removed).
明确解除绑定
$("#StartRRP").removeClass('customreadonly');
$("#StartRRP").unbind('keydown');
或更改使用授权的方法
$(document).on("keydown", ".customreadonly", function () {
return false;
});
这篇关于使用jquery启用和禁用键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!