跳转到主要内容

主页内容

Drupal中的JavaScript相关操作

由 webadmin 发布于 阅读 9 次

1、从module中传值到JavaScript中

(1)、在模块根目录下创建demo_form.module文件,在文件中使用hook进行传值

function demo_form_preprocess_page(&$variables) {
  $variables['#attached']['library'] = 'demo_form/demo_form_js_css';
  $variables['#attached']['drupalSettings']['webInfo']['url'] = 'https://google.com';
  $variables['#attached']['drupalSettings']['webInfo']['other'] = 'https://www.youtube.com/';
}

(2)、在JavaScript中输出值

(function ($,Drupal,drupalSettings) {
  'use strict';
  $(document).ready(function () {})
  Drupal.behaviors.DemoForm = {
    attach : function (context, settings) {
      let webUrl = drupalSettings.webInfo.url;
      let webOther = drupalSettings.webInfo.other;
      console.log('webUrl:'+webUrl);
      console.log('webOther:'+webOther);
    }
  };
})(jQuery, Drupal, drupalSettings)

2、先在JavaScript中完成表单验证,然后再提交表单