使用jQuery锁定一个textarea? [英] locking a textarea using jQuery?
本文介绍了使用jQuery锁定一个textarea?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用jQuery锁定"文本区域,以便不能再输入任何字符?我不想禁用它,因为我想允许删除字符.
How do I "lock" a textarea with jQuery so that no more characters can be entered? I don't want to necessarily disable it since I want to allow characters to be deleted.
更新:哎呀..它刚来找我: 如果我将长度限制为说400个字符,那么当长度> 400时我可以使用以下内容:
update: oops..it just came to me: if I am limiting the length to say 400 characters then i can use the following when the length is > 400:
this.value = this.value.substring(0, 400);
这将减少多余的部分
推荐答案
以下是修改jquery.numeric插件的快速反应:)
following is quick contraption from modification of jquery.numeric plugin :)
它允许特殊键,但不允许用户输入任何内容.
It allows special keys but don't let user type anything.
<textarea id="txt" rows="5" cols="50"></textarea>
<script type="text/javascript">
$(document).ready(function(){
$("#txt").keypress(function(e){
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
// allow Ctrl+A
if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65)
/* opera */) return true;
// allow Ctrl+X (cut)
if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88)
/* opera */) return true;
// allow Ctrl+C (copy)
if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67)
/* opera */) return true;
// allow Ctrl+Z (undo)
if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90)
/* opera */) return true;
// allow or deny Ctrl+V (paste), Shift+Ins
if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86)
/* opera */
|| (e.shiftKey && key == 45)) return true;
//page up, down, home end, left-right-up-down
if(key > 32 && key < 40) return true;
// if DEL or BACKSPACE is pressed
return key == 46 || key == 8;
});
});
</script>
这篇关于使用jQuery锁定一个textarea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文