如何将此 JavaScript 代码插入到我的 template.tpl.php 中来修改评论表单? (函数XX_comment_form DRUPAL)

发布于 2024-12-15 14:55:17 字数 1984 浏览 4 评论 0原文

我有一个 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);
}

希望任何人都可以在这里帮助我,因为我几乎一无所知。

谢谢!

罗莎蒙达

I´ve got a Drupal site and I need to limit comments length.
So, I´ve found this code:

Inside ghead I should add this:

<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>

And I should add this stuff where I put the comment body:

<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>

My problem is that I have no idea on where to put this actually.
I think this goes inside template.tpl.php file.

I´ve found a custom example, but don´t know how to insert the code above:

/**
* 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);
}

Hope anyone can help me out here, because I´m pretty much clueless.

Thanks!

Rosamunda

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2024-12-22 14:55:17

将您的 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>"

Put your javascript code to comment.tpl.php or other template file. And in mytheme_comment_form($form) function add attributes to your 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>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文