簡體   English   中英

如何在我的template.tpl.php中插入此javascript代碼以修改評論表單? (功能XX_comment_form DRUPAL)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM