jQuery 如何使用jquery从输入中删除按键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1564072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:43:55  来源:igfitidea点击:

How to remove keypress from input with jquery?

jquery

提问by Jin Yong

Does anyone know how can I remove keypress from input with jquery?

有谁知道如何使用 jquery 从输入中删除按键?

Example:

例子:

<input type="text" keypress="cleanMsg()" id="cleanMsg" name="cleanMsg">

How can I removed the keypress="cleanMsg()" from input by using jquery? Or anyway to replace keypress="cleanMsg()" to keypress="Msg()"??

如何使用 jquery 从输入中删除 keypress="cleanMsg()" ?或者无论如何将 keypress="cleanMsg()" 替换为 keypress="Msg()"??

采纳答案by wiifm

I assume you mean "how do you remove the 'keypress' attribute from the input", in which case this would work

我假设你的意思是“你如何从输入中删除‘keypress’属性”,在这种情况下这会起作用

<script>
    $(document).ready(function(){
        $("#cleanMsg").removeAttr("keypress");
    });
</script>

If you want to bind to a keypress, you should do it in the jQuery way (refer to the docs), with this code the above attribute within the input tag is redundant

如果你想绑定到一个按键,你应该用 jQuery 的方式来做(参考文档),使用这段代码,输入标签中的上述属性是多余的

<script>
    $(document).ready(function(){
        $("#cleanMsg").keypress(function(){
            //do something here
        });
    });
</script>

回答by Kai Carver

To further answer the general question, you can remove any keypress handlers added with Jquery with:

要进一步回答一般问题,您可以删除任何使用 Jquery 添加的按键处理程序:

$('#foo').unbind("keypress");

unbind() without arguments removes all handlers.

不带参数的 unbind() 删除所有处理程序。

回答by Taha Jahangir

use unbind method

使用解绑方法

$('#foo').unbind();

http://api.jquery.com/unbind/

http://api.jquery.com/unbind/