简体   繁体   English

Drupal 6至7:添加jquery函数以形成表单

[英]Drupal 6 to 7: add jquery function to form

I need to convert code from Drupal 6 to Drupal 7 but stuck at the part for jquery. 我需要将代码从Drupal 6转换为Drupal 7,但停留在jquery部分。 In Drupal 6, the code to add jquery is as the following in which simpleColor is a function in jquery.simple-color.min.js : 在Drupal 6中,添加jquery的代码如下所示,其中simpleColorjquery.simple-color.min.js一个函数:

drupal_add_js(drupal_get_path('module', 'module_name') . '/js/jquery.simple-color.min.js', 'module', 'header', FALSE, FALSE, FALSE);

$jquery = '
  $(document).ready(function() {
    $(".simple_color").simpleColor({
      displayColorCode: true,
      boxWidth: "6em",
      defaultColor: "#36c",
    });
  });';

drupal_add_js($jquery, 'inline');

The simple_color has been added into form: simple_color已添加到表单中:

$form['feeds']["feed_$fid"]['color'] = array(
  '#title' => t('Color'),
  '#type' => 'textfield',
  '#size' => 10,
  '#attributes' => array(
    'class' => 'simple_color',
  ),
);

I has tried to convert the above code to Drupal 7 but unsuccessful: 我试图将以上代码转换为Drupal 7,但未成功:

drupal_add_js(drupal_get_path('module', 'module_name').'/js/jquery.simple-color.min.js', array('type'=>'module', 'scope'=>'header', 'defer'=>FALSE, 'cache'=>FALSE, 'preprocess'=>FALSE));

$jquery = '
  jQuery(document).ready(function() {
    $(".simple_color").simpleColor = {
      attach: function({
        displayColorCode: true,
        boxWidth: "6em",
        defaultColor: "#36c"
    })
  };
});';

drupal_add_js($jquery, array('type'=>'inline'));

In form: 通知:

$form['feeds']["feed_$fid"]['color'] = array(
  '#title' => t('Color'),
  '#type' => 'textfield',
  '#size' => 10,
  '#attributes' => array(
    'class' => array('simple_color'),
  ),
);

I have try many ways but they didn't work for this case. 我尝试了很多方法,但是在这种情况下它们没有用。 Please take sometime to have a look and try to help me. 请花些时间看看并尝试帮助我。 Thanks ahead. 谢谢你

Applying the suggestion from 2pha, I have successfully added jquery code to drupal. 应用2pha的建议,我已成功将jquery代码添加到drupal。 The following is the fix in drupal 7: 以下是drupal 7中的修复程序:

drupal_add_js(drupal_get_path('module', 'module-name') . '/js/jquery.simple-color.min.js','file');

$jquery = '
  (function($) {
  Drupal.behaviors.myBehavior = {
    attach: function (context, settings) {
      $(document).ready(function() {
        $(".simple_color").simpleColor({
          displayColorCode: true,
          boxWidth: "6em",
          defaultColor: "#36c",
        });
      });
    }
  };
  })(jQuery);';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM