在Drupal中,如何使用js对CCK "Add another item"按钮做出反应?



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

但是,我只知道如何对$(document)时存在的字段作出反应。准备好了。我不知道如何检测新字段的创建(即那些在CCK无限值字段中使用"添加另一个项目"按钮创建的字段),以便也改变该字段。

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

如果你使用的是更高版本的jQuery,你可以使用。live函数,但由于Drupal在jQuery上有点落后,你将需要使用Drupal behavior。当从DOM中插入/删除新项时,您想要运行的任何javascript代码都必须在一个行为中声明,以便在Drupal时触发它。

在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}
};

相关内容

最新更新