【问题标题】:How to include JS in Contact Form 7 In Wordpress如何在 Wordpress 的联系表 7 中包含 JS
【发布时间】:2021-01-08 03:06:24
【问题描述】:

我的这段代码有问题。我希望你们能在这件事上帮助我。我在这里要做的是验证 Contactform 7 的 [输入文本],用户只能在输入中键入数字。我有这个作为参考Including JS to Contact Form 7 Wordpress 这样做。所以我想在我的函数 php 上创建这个脚本的简码,然后按照上面链接中的建议在联系表单中使用这个简码。这是我目前在我的functions.php中遇到此代码的错误

由于文件 wp-content/themes/capitall/functions.php 的第 262 行出现错误,您的 PHP 代码更改已回滚。请修复并再次尝试保存。 语法错误,意外'

function input_cf7() {

<script>

        var userName = document.querySelector('#numberField');

        userName.addEventListener('input', restrictNumber);
        function restrictNumber (e) {  
        var newValue = this.value.replace(new RegExp(/[^\d]/,'ig'), "");
        this.value = newValue;

        }

</script>

}

add_shortcode('input_validation', 'input_cf7');

【问题讨论】:

  • 您正在混合 PHP 和 JS 代码。要么用&lt;?php ... ?&gt;包围你的PHP,要么用echo输出你的JS。

标签: javascript php wordpress shortcode contact-form-7


【解决方案1】:

脚本标签在html中,它不会在php块中运行,所以你必须在脚本标签之前关闭你的php块并在脚本标签之后打开php块

【讨论】:

    猜你喜欢
    • 2017-06-24
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2020-05-30
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多