【发布时间】:2012-05-03 05:10:37
【问题描述】:
我正在使用一个限制 textArea 长度的函数。虽然函数执行得很好,但我想问一下我是否以正确的方式调用函数?
在这里
<h:inputTextarea id="questionInputTextArea"
value="#{faqAddUpdate.question}"
cols="50"
rows="3"
disabled="#{faqAddUpdate.buttonState}"
onkeypress="limitTextArea(this, 400)"
style="overflow: auto;">
<f:validateLength maximum="200" />
</h:inputTextarea>
这里是函数
function limitTextArea(element, limit) {
var $textArea = $(element); //casting to jQuery so we can use jQuery functions on it
($textArea).keypress(function(event){
if (event.which < 0x20) {
// e.which < 0x20, then it's not a printable character
// e.which === 0 - Not a character
return; // Do nothing
}
if ($textArea.val().length == limit) {
event.preventDefault();
} else if ($textArea.val().length > limit) {
// Maximum exceeded
$textArea.val() = $textArea.val().substr(0, limit);
}
}); //end of keypress
} //end of function()
让我感到困惑的是,我在我的 jsf 代码中使用 onkeypress,并且在函数中我还说 ($textArea).keypress(function(event) 。我该如何优化这个功能?或者我做得对吗?
谢谢
【问题讨论】:
标签: javascript jquery jsf-2