首页 > 解决方案 > 在 jquery 中添加联系 form7 简码

问题描述

我正在尝试在我的 jquery 函数中通过简码插入 cf7。在我的脚本中,它就像命中:

    <script type="text/javascript">
      jQuery(document).on("click", ".helpful #smthWrong", function (e) {
        jQuery(".helpful ").html(<?php echo do_shortcode("[contact-form-7 id='107' title='Kontaktų forma 1']"); ?>);
        });
        </script>

当我签入控制台时,出现意外令牌 < 错误,结果如下所示:

jQuery(".helpful ").html(<div role="form" class="wpcf7" id="wpcf7-f107-o1" lang="lt-LT" dir="ltr">

当我将我的 php 代码包装在 ' ' 中时,我得到了 Uncaught SyntaxError: Invalid or unexpected token 并且结果如下所示:

jQuery(".helpful ").html('<div role="form" class="wpcf7" id="wpcf7-f107-o1" lang="lt-LT" dir="ltr">

我读到可以使用 ajax 添加短代码,但也许有解决方案以这种方式添加短代码?

标签: wordpressshortcodecontact-form-7

解决方案


I don't think you can achieve this using jquery as jquery will only work after the webpage is loaded into the browser, so even if you add a shortcode using jquery it will be of no use as the shortcodes are rendered on server and not in browser.

But you can achieve this using ajax.


推荐阅读