【问题标题】:Including JS to Contact Form 7 Wordpress包括 JS 到 Contact Form 7 Wordpress
【发布时间】:2017-11-02 21:31:19
【问题描述】:

您可能认为问题在于将 js 包含到著名的 wordpress 插件 - Contact Form 7 中。

我的表单如下所示:

<label>[text* your-name] </label>
<label>[email* your-email]</label>
<label>[number number-189 min:500000000 max:900000000 id:numertel class:numertel1]</label> //this is telephone number
<label>[text* your-subject] </label>
<label>[textarea* your-message] </label>
[submit id:przycisk "Send"]

stackoverflow 的用户帮助我修复了应该启用的 JS 代码 电话号码长度为 9 或 10 时的提交按钮。

这里是JS代码:

document.getElementById("przycisk").disabled = true;
document.getElementById("numertel").addEventListener("keyup", function() {
  var numerlength = this.value.length;
  if (numerlength == 10 || numerlength == 9)
    document.getElementById("przycisk").disabled = false;
  else
    document.getElementById("przycisk").disabled = true;
});

以及我从浏览器中的“检查元素”选项中看到的插件生成的 HTML:

<input type="number" name="number-189" value="" class="wpcf7-form-control wpcf7-number wpcf7-validates-as-number numertel1" id="numertel" min="500000000" max="900000000" aria-invalid="false">
<input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit" id="przycisk">

代码运行完美,但我不知道如何将他包含到插件中,因为这样的东西不起作用:

<label>[text* your-name] </label>
<label>[email* your-email]</label>
<label>Telephone[number number-189 min:500000000 max:900000000 id:numertel class:numertel1]</label>
<label>[text* your-subject] </label>
<label>[textarea* your-message] </label>
[submit id:przycisk "Send"]
<script>
document.getElementById("przycisk").disabled = true;
document.getElementById("numertel").addEventListener("keyup", function() {
  var numerlength = this.value.length;
  if (numerlength == 10 || numerlength == 9)
    document.getElementById("przycisk").disabled = false;
  else
    document.getElementById("przycisk").disabled = true;
});
</script>

【问题讨论】:

    标签: javascript html wordpress plugins contact-form-7


    【解决方案1】:

    您可以为给定的 JS 代码创建额外的短代码,并将其插入到联系表单 7 标记之后(不是插入,而是插入之后)。

    所以它看起来像这样

    [contact-form-7 id="122" title="联系我们"] [custom_js_for_cf7]

    这里是这个的简码,你可以把它添加到你的functions.php中:

    add_shortcode('custom_js_for_cf7','custom_js_for_cf7');
    function custom_js_for_cf7($args){
    return '
    <script>
    document.getElementById("przycisk").disabled = true;
    document.getElementById("numertel").addEventListener("keyup", function() {
      var numerlength = this.value.length;
      if (numerlength == 10 || numerlength == 9)
        document.getElementById("przycisk").disabled = false;
      else
        document.getElementById("przycisk").disabled = true;
    });
    </script>
    ';
    }
    

    【讨论】:

    • 毫无疑问我会检查一下,但是如何为联系表格 7 标签创建额外的短代码?
    • “为联系表格 7 创建额外的简码”是什么意思?为了什么?
    • "您可以为给定的 JS 代码创建额外的短代码" 所以它看起来像这样 [contact-form-7 id="122" title="联系我们"] [custom_js_for_cf7]
    • 我已经为那个额外的短代码提供了所需的代码。您只需将该代码粘贴到主题的 functions.php 中,就可以了,额外的短代码开始工作
    • 我想我必须在我创建的表单中写下这个 [custom_js_for_cf7] 来通知 add_shortcode ,好的,非常感谢
    猜你喜欢
    • 2020-04-09
    • 2021-07-23
    • 2020-01-03
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    相关资源
    最近更新 更多