I have a long questionnaire as multi-page webform and I do many mostly simple counting and addclass using jquery done via a custom module I made for the purpose. This was perfectly working before on Drupal 8 with WF x5 but not anymore after upgrading to Webform x6 (dev) on latest Drupal 9.02.

The below sample code only works if I move the related form to be on first page while it doesn't load on later pages!

(function($, load) {
    $('input.countme').change(function(){   
        var a =  $("input.countme:checked").length // count all checked checkboxes with the css class "countme"
        $(".cm_score").val(a)  //update the value of the textfield that has the class ".cm_score" 
    }); 
})(jQuery);

Below is myModule.libraries.yml

wfcount:
  version: VERSION
  css:
    theme:
      css/webform.css: { preprocess: false }
  js: 
    js/wfCount.js: { preprocess: false }    
  dependencies:
    - core/jquery
有帮助吗?

解决方案

Following the tips in this post, I changed the code as follows and it worked:

(function ($, Drupal) {
 Drupal.behaviors.myBehavior = {
  attach: function (context, settings) {
    $('input.countme').change(function(){   
        var a =  $("input.countme:checked").length // count all checked checkboxes with the css class "countme"
        $(".cm_score").val(a)  //update the value of the textfield that has the class ".cm_score" 
    }); 
 }
};
})(jQuery, Drupal);
许可以下: CC-BY-SA归因
scroll top