第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 AJAX 網(wǎng)站上使用動(dòng)態(tài)注入的 Ninja Forms

在 AJAX 網(wǎng)站上使用動(dòng)態(tài)注入的 Ninja Forms

PHP
慕婉清6462132 2023-08-19 14:08:57
我正在構(gòu)建一個(gè) Wordpress 網(wǎng)站,并使用 AJAX 在用戶導(dǎo)航時(shí)加載后續(xù)頁(yè)面,以便頁(yè)面轉(zhuǎn)換具有精美的動(dòng)畫(huà)效果。默認(rèn)情況下,如果您加載并注入嵌入了 Ninja Form 的頁(yè)面,則該表單根本不會(huì)顯示。關(guān)于如何實(shí)現(xiàn)這一目標(biāo)的信息非常少。我希望有一個(gè)官方的開(kāi)箱即用的方式來(lái)實(shí)現(xiàn)這一點(diǎn),但似乎沒(méi)有。當(dāng)使用 AJAX 動(dòng)態(tài)加載表單時(shí),需要采取哪些步驟才能使表單顯示在頁(yè)面上?
查看完整描述

1 回答

?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

我嘗試了一些完全未記錄的代碼示例,并設(shè)法弄清楚了它,以及一些必要的調(diào)整和添加。我想我會(huì)在這里分享,以防其他人遇到困難。


步驟 1. 將必要的 JS 和 CSS 入隊(duì)


將以下內(nèi)容添加到您的functions.php中,因?yàn)镹inja Forms依賴于主干js,并且需要CSS,如果您的初始登陸頁(yè)面上還沒(méi)有表單,則不會(huì)加載CSS。


add_action('wp_enqueue_scripts', function () {

  // enqueue ninja forms css, including for any ninja forms addons

  wp_enqueue_style('nf-display', content_url('/plugins/ninja-forms/assets/css/display-structure.css'), ['dashicons'], core_dev_version(''));

  wp_enqueue_style('nf-layout-front-end', content_url('/plugins/ninja-forms-style/layouts/assets/css/display-structure.css'), ['nf-display'], core_dev_version(''));


  // make sure that backbone is enqueued on the page, as ninja forms relies on this

  wp_enqueue_script('backbone');

}, 100);

.


步驟 2. 添加返回表單數(shù)據(jù)的 AJAX 函數(shù)


您不需要編輯它,只需將其添加到您的functions.php 中即可。我們?cè)诓襟E 3 中使用的 javascript 將進(jìn)行自己的 AJAX 調(diào)用,以非常特定的格式請(qǐng)求一些表單數(shù)據(jù)。


add_action('init', function () {

  // if this is not an AJAX form request, return

  if (! isset($_REQUEST[ 'async_form' ])) {

    return;

  }


  // clear default loaded scripts.

  global $wp_scripts;

  unset($wp_scripts->registered);


  // get the requested form id

  $form_id = absint($_REQUEST['async_form']);


  // retrieve the requested form

  ob_start();

  $form = do_shortcode("[ninja_forms id='{$form_id}']");

  ob_get_clean();


  // output the requested form on the page

  ob_start();

  NF_Display_Render::output_templates();

  $templates = ob_get_clean();

  $response = [

    'form' => $form,

    'scripts' => $wp_scripts->registered,

    'templates' => $templates

  ];

  echo json_encode($response);


  // die, because we don't want anything else to be returned

  die();

});

.


步驟 3. 將 JS 輔助函數(shù)添加到您的登陸頁(yè)面


這只是將一些有用的 JS 代碼添加到您的站點(diǎn)中。您可以將其按原樣添加到functions.php,或?qū)⑵浒趩为?dú)的JS文件中。這就是我們將用來(lái)加載和初始化我們想要的表單的方法。


add_action('wp_footer', function () {

  // adds a script to the footer

  ?>

  <script type="text/javascript">


  var NinjaFormsAsyncForm = function(formID, targetContainer) {


    this.formID = formID;

    this.targetContainer = targetContainer;


    this.formHTML;

    this.formTemplates;

    this.formScripts;


    this.fetch = function(callback) {

      jQuery.post('/', { async_form: this.formID }, this.fetchHandler.bind(this))

      .then(callback);

    }

    this.fetchHandler = function(response) {

      response = JSON.parse( response );


      window.nfFrontEnd = window.nfFrontEnd || response.nfFrontEnd;

      window.nfi18n = window.nfi18n || response.nfi18n || {};


      this.formHTML = response.form;

      this.formTemplates = response.templates;

      this.formScripts = response.scripts;

    }


    this.load = function() {

      this.loadFormHTML(this.formHTML, this.targetContainer);

      this.loadTemplates(this.formTemplates);

      this.loadScripts(this.formScripts);

    }


    this.loadFormHTML = function(form, targetContainer) {

      jQuery(targetContainer).append( form );

    }

    this.loadTemplates = function(templates) {

      document.body.innerHTML += templates;

    }

    this.loadScripts = function(scripts) {

      jQuery.each( scripts, function( nfScript ){

        var script = document.createElement('script');

        // note that eval() can be dangerous to use - do your research

        eval( scripts[nfScript].extra.data );

        window.nfFrontEnd = window.nfFrontEnd || nfFrontEnd;

        script.setAttribute('src',scripts[nfScript].src);

        var appendChild = document.head.appendChild(script);

      });

    }


    this.remove = function() {

      jQuery(this.targetContainer).empty();

    }

  }


  </script>

  <?php

}, 100);

.


步驟 4. 在頁(yè)面中使用 AJAX 后初始化表單


我無(wú)法告訴您如何在頁(yè)面中使用 AJAX - 這是您需要弄清楚的另一個(gè)主題。但是,一旦您使用包含的 Ninja Form 加載了頁(yè)面內(nèi)容,并且成功位于頁(yè)面上,現(xiàn)在您需要初始化該表單。


這使用了 javascript 幫助程序(步驟 3),該幫助程序又調(diào)用 php 幫助程序(步驟 2),最后在您的頁(yè)面上顯示表單!


您需要知道已注入表單的 ID。我的策略是將其作為數(shù)據(jù)屬性包含在我的頁(yè)面標(biāo)記中。


var form_id = 1;

var asyncForm = new NinjaFormsAsyncForm(form_id, '.ninja-forms-dynamic');

asyncForm.fetch(function () {

  asyncForm.load();

});

這就是全部?jī)?nèi)容了!


希望這可以節(jié)省其他人解決所有這些問(wèn)題的時(shí)間和精力。


查看完整回答
反對(duì) 回復(fù) 2023-08-19
  • 1 回答
  • 0 關(guān)注
  • 214 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)