【问题标题】:In Drupal, how can I use js to react to the CCK "Add another item" button?在 Drupal 中,如何使用 js 对 CCK“添加另一个项目”按钮做出反应?
【发布时间】:2011-07-12 02:36:19
【问题描述】:

我正在附加一些改变字段的 js(即,根据在另一个选择字段中选择的内容来限制选择字段的选项)。

但是,我只知道如何对调用 $(document).ready 时存在的字段做出反应。我不知道如何检测新字段的创建(即使用 CCK 无限值字段中的“添加另一个项目”按钮创建的字段)以更改该字段。

与 Drupal 中的所有内容一样,答案要么是 jQuery 中非常基本的东西,要么是非常复杂的东西。我是 jQuery 新手,所以我希望是前者。

【问题讨论】:

    标签: jquery forms drupal


    【解决方案1】:

    如果您使用的是更高版本的 jQuery,您可以使用 .live 函数,但由于 Drupal 在 jquery 上有些落后,您需要使用 Drupal Behaviors。在从 DOM 插入/删除新项目时要运行的任何 javascript 代码都必须在行为侧声明,以便在调用 Drupal.attachBehaviors 时触发。

    在 Drupal 6 中,它看起来像:

    Drupal.behaviors.myModule = function (context) {
      //Code here is run when the DOM is updated (if Drupal.attachBehaviors is called)
    };
    

    在 Drupal 7 中:

    Drupal.behaviors.myModule = {
     attach: function (context, settings) { // code here for when items are attached},
     detach: function (context, settings) { // code here for when items are detached}
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多