开发者

In Drupal, how can I use js to react to the CCK "Add another item" button?

开发者 https://www.devze.com 2023-03-19 07:48 出处:网络
I\'m appending a bit of js that alter fields (namely, limits the option of select fields based on what has been chosen in another select field).

I'm appending a bit of js that alter fields (namely, limits the option of select fields based on what has been chosen in another select field).

However, I only know how to react to fields that exist when $(document).ready is called. I don't know how to detect the creation of a new field (namely, those created with the "Add another item" button in CCK unlimited value fields) in order to also alter that field.

As with everything in Drupal, the answer is either something really basic in jQuery or something really com开发者_如何学JAVAplex. I'm a jQuery novice, so I'm hoping it's the former.


If you were working with a higher version of jQuery you could use the .live function but since Drupal lags a bit behind on jquery you will need to use Drupal Behaviors. Any javascript code that you want to run when new items are inserted/removed from the DOM must be declared in side a behavior so it will be triggered when Drupal.attachBehaviors is called.

In Drupal 6 it would look something like:

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

and in 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}
};
0

精彩评论

暂无评论...
验证码 换一张
取 消