10

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 to be hidden or shown.I tried to get a result like console.log('trigger'); when clicked or something else but nothing in dev. console appeared.Also, I tried the following methods:

To call the click event with .on('click', function()... or to call the event with .trigger('click');, or to change the event to change

To embed the script within a file from ninja forms or to put it inside the page at the ending of body tag in footer.php

To change the opening declaration of jQuery to work inside a function like this : (function($) {$(document).ready(function(){.....

I know that I could try another plugin, I tried one and the custom jQuery works but I really like this one and don't know why this is happening ...

Thanks

4

2 回答 2

44

不确定您是否需要帮助,因为您发布问题已经有一段时间了,但这可能会在未来对其他人有所帮助。我遇到了相同/类似的问题,无法在 Ninja Forms 上运行 JS/jQuery,发现这是因为 Ninja Forms 异步加载表单。因此,当您的 document.ready 函数运行时,表单还不存在并且无法绑定。

Ninja Forms 有自己的事件就绪状态,可以按如下方式使用:

jQuery(document).on( 'nfFormReady', function( e, layoutView ) {
    // Your code goes here...
});
于 2017-08-02T12:38:57.067 回答
0

该事件未注册仅仅是因为您尝试绑定该事件的元素当时还不存在(在文档加载时)。Ninja 表单异步加载表单内容,因此您必须等到表单完全加载后再添加事件侦听器。这对我有用:

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
于 2017-03-09T11:39:27.120 回答