【问题标题】:Contact form 7 : multiple lines of javascript on submit联系表 7:提交时多行 javascript
【发布时间】:2016-12-01 17:14:43
【问题描述】:

我需要使用 Contact 7 来获得一个嵌入式表单,将信息提交到我的 Viral Loops 帐户。

表单提交后需要运行如下代码,即on_sent_ok:

VL.options.form_fields.form_firstName = $("#firstname").val(); //capture the first name
VL.options.form_fields.form_email = $("#email").val(); //capture the email
VL.options.form_fields.form_lastName = $("#lastname").val(); //capture the last name (if applicable in your form)
//submit the participant to Viral Loops
VL.createLead(function() {
//any logic to run after the participation
});

我不知道在哪里或如何添加它,因为您只能在高级设置中使用 on sent ok 的一行代码。

提前致谢!! :)

【问题讨论】:

  • 这不是最可读的,但是为什么不把你所有的js放在一行上呢?分号将负责区分任何一条语句的结尾。
  • 那个或者你可以把它作为一个函数放在你已经入队的外部 JS 文件中 (developer.wordpress.org/reference/functions/wp_enqueue_script) 并从 on_sent_ok 调用该函数

标签: javascript wordpress forms contact-form-7


【解决方案1】:

如 cmets 中所述,要么将其缩小:

on_sent_ok: "VL.options.form_fields.form_firstName = $('#firstname').val();VL.options.form_fields.form_email = $('#email').val();VL.options.form_fields.form_lastName = $('#lastname').val();VL.createLead(function() {});"

或者您可以在js/script.js 的(子)主题目录中创建一个JavaScript 文件,然后将其添加到您的(子)主题目录中的functions.php

/**
 * Enqueue a script with jQuery as a dependency.
 */
function so_40916565_enqueue() {
    wp_enqueue_script( 'so-40916565', get_stylesheet_directory_uri() . '/js/script.js', array( 'jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'so_40916565_enqueue' );

在您新创建的 javascript 文件中:

function js_40916565() {
  VL.options.form_fields.form_firstName = $("#firstname").val(); //capture the first name
  VL.options.form_fields.form_email = $("#email").val(); //capture the email
  VL.options.form_fields.form_lastName = $("#lastname").val(); //capture the last name (if applicable in your form)
  //submit the participant to Viral Loops
  VL.createLead(function() {
    //any logic to run after the participation
  });
}

在您的联系表格 7 中:

on_sent_ok: "js_40916565();"

我还没有实际测试过,所以如果开箱后不能正常工作,请发表评论。

【讨论】:

  • 它不工作 :( 我的 js 应该在一开始就有这个吗?<?php /** * Enqueue 如果有帮助,这些是我的联系人 7 个字段<p>[text* firstname placeholder "First Name*"]</p> <p></p> <p>[text* lastname placeholder "Last Name*"]</p> <p></p> <p>[email* email placeholder "Email Address*"]</p> <p></p> <p>[text* country placeholder "Country*"]</p> <p></p> <p>[submit "Send"]</p> 我还在脚本中添加了国家行VL.options.form_fields.form_country = $("#country").val(); //capture the country (if applicable in your form)
  • 不,抱歉。也许我的帖子不够清楚。 function js_40916565 块中的代码应该在您的js/script.js 文件中,仅此而已(除非该文件已经存在)。 Enqueue a script with jQuery as a dependency 块应该在您的 functions.php 文件中,并且 on_sent_ok 显然应该在 WordPress 管理员的联系表 7 中。
  • 是的,我已经这样做了,但是我使用的是 The Gem 主题,它在主题选项中有自定义 js - 我在那里添加了 function 位。 enqueue 位在我的functions.php 文件中,在文件中已经有<?php - 如果我删除它,网站顶部会出现错误。 On sent ok位在联系人7高级设置中。如果有帮助,请点击此处的页面 [链接] (sportshosts.com/home-13-2)
  • 是的,该链接确实有帮助。由于您在主题选项中添加了javascript函数,因此无需在functions.php文件中添加代码,实际上您应该从functions.php中删除代码,因为网站会尝试加载文件js/script.js那不存在。最后,您还没有更新联系表 7 中的代码。将 on_sent_ok 更改为 on_sent_ok: "js_40916565();"。你快到了!哦,您将不得不在您的 CF7 字段中添加 ID。示例[email* email id:EMAIL placeholder "Email Address*"]
  • 还是没有运气。病毒循环的人给了我一些 js 代码 function js_40916565() { VL.options.form_fields.form_firstName = jQuery("#firstname").val(); VL.options.form_fields.form_email = jQuery("#email").val(); VL.options.form_fields.form_lastName = jQuery("#lastname").val(); VL.extraData["rNk0t3ZC"] = jQuery("#country").val(); VL.createLead(function() { //any logic to run after the participation }); } Annnd 我从函数文件中取出了东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
相关资源
最近更新 更多