我想限制一个人可以输入内容类型正文的字数。并向用户显示字数。
做这个的最好方式是什么。
最简单的解决方案是安装这个 drupal 模块:
https://drupal.org/project/maxlength
如果您希望手动执行此操作,您可以尝试以下解决方案:
将此添加到您的表单字段定义中:
'#attributes' => array('onKeyPress' => "return textareaMaxLength(this, event, $sms_max_len);"),
将新的 JS 文件添加到您的主题中:
drupal_add_js(drupal_get_path('module', 'mymodule'). '/myfile.js');
Myfile.js: 函数 textareaMaxLength(field, evt, limit) { var evt = (evt) ? evt:事件;var charCode = (typeof evt.which != "undefined") ? evt.which : ((typeof evt.keyCode != "undefined") ? evt.keyCode : 0);
if (!(charCode >= 13 && charCode <= 126)) {
return true;
}
return (field.value.length < limit);
}
脚本/参考的信用: https ://drupal.org/node/80122