我有一个Drupal站点,我需要限制评论的长度。所以,我找到了这段代码:
Inside ghead我应该加上这个:
<script language=”javascript”>
function limit(what,chars,counter) {
if (what.value.length > chars) {
what.value=what.value.substr(0,chars);
alert(‘You exceed to ‘ + chars + ‘chars!’);
}
counting = (chars – what.value.length);
c = document.getElementById(counter);
c.innerHTML = counting;
}
</script>
我应该在我放注释主体的地方加上这些东西:
<p><label for=”text”><strong>Text</strong></label> ¦ Chars left: <span id=”count1″>500</span></p>
<textarea name=”[1][2][t]” rows=”10″ cols=”50″ onkeyup=”limit(this,500,’count1′);” onkeydown=”limit(this,500,’count1′);” onblur=”limit(this,500,’count1′);” onfocus=”limit(this,500,’count1′);”></textarea>
我的问题是,我不知道在哪里把这个实际上。我想这应该放在template.tpl.php文件中。
我找到了一个自定义示例,但不知道如何插入上面的代码:
/**
* Theme the output of the comment_form.
*
* @param $form
* The form that is to be themed.
*/
function mytheme_comment_form($form) {
// Rename some of the form element labels.
$form['name']['#title'] = t('Name');
$form['homepage']['#title'] = t('Website');
$form['comment_filter']['comment']['#title'] = t('Your message');
// Add some help text to the homepage field.
$form['homepage']['#description'] = t('If you have your own website, enter its address here and we will link to it for you. (please include http://).');
$form['homepage']['#description'] .= '<br/>'. t('eg. http://www.kirkdesigns.co.uk');
// Remove the preview button
$form['preview'] = NULL;
return drupal_render($form);
}
希望有人能帮助我,因为我几乎没有头绪。
谢谢!
Rosamunda
将javascript代码放入comment.tpl.php或其他模板文件中。并在mytheme_comment_form($form)函数添加属性到您的textarea:
$form['textareaname']['#attributes'] = array('onkeyup' => 'limit(this,500,’count1′)';
$form['textareaname']['#title'] = "<label for=”text”><strong>Text</strong></label> ¦ Chars left: <span id=”count1″>500</span>"