Custom jQuery not working on Wordpress Ninja Forms plugin

后端 未结 2 1111
再見小時候
再見小時候 2021-02-08 23:17

I am having some problems when I want to add custom jQuery code that affects the form.

For example when someone clicks an input or radio button another input or element

相关标签:
2条回答
  • 2021-02-08 23:50

    The event isn't registered simply because the elements you're trying to bind the event to do not exist yet at that moment (on document load). Ninja forms loads the form contents asynchronously, so you'll have to wait until the form is fully loaded and then add your event listeners. This works for me:

    var formExists = setInterval(function() {
      if ($(".nf-form-cont").length) {
        // Set your event listeners here, example:
        $("#nf-field-1").click(function(e) {
          console.log("click!");
        }
        clearInterval(formExists);
      }
    }, 100); // check every 100ms
    
    0 讨论(0)
  • 2021-02-08 23:55

    Not sure if you need help with this any more as it's been some time since you posted your question, but this may help others in the future. I had the same/similar issue with not being able to run JS/jQuery on the Ninja Forms and found that it's because Ninja Forms load their forms asynchronously. So, when your document.ready function runs, the form doesn't yet exist and it's not able to bind.

    Ninja Forms have their own event ready state that can be used as follows:

    jQuery(document).on( 'nfFormReady', function( e, layoutView ) {
        // Your code goes here...
    });
    
    0 讨论(0)
提交回复
热议问题