开发者

How to remove keypress from input with jquery?

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

Example:

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

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


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

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

unbind() without arguments removes all handlers.


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

<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

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


use unbind method

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜