[英]How to insert this javascript code inside my template.tpl.php to modify the comment form? (function XX_comment_form DRUPAL)
我有一个Drupal网站,我需要限制评论的长度。 因此, 我找到了以下代码:
在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);
}
希望有人能在这里帮助我,因为我几乎一无所知。
谢谢!
罗萨蒙达
将您的JavaScript代码放入comment.tpl.php或其他模板文件。 并在mytheme_comment_form($ form)函数中向文本区域添加属性:
$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>"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.