2016-12-01 64 views
0

我需要使用联系人7有一个嵌入表单,将信息提交给我的Viral Loops帐户。联系表格7:提交javascript的多行

它需要一次表单提交给运行下面的代码,即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 
}); 

我不知道在哪里或如何添加它,因为您也可以利用一行代码只有在高级设置中发送确定。

在此先感谢! :)

+1

它不是最易读的,但为什么不把所有的js放在一行呢?分号将关注区分任何一个语句的结束。 –

+0

这或你可以把它作为一个函数在你已经入队的外部JS文件(https://developer.wordpress.org/reference/functions/wp_enqueue_script/)并从on_sent_ok调用函数 – Jonathan

回答

2

正如在评论中提到,无论是再压缩它:

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();" 

我没有实际测试过,所以如果它不工作的权利开箱,发表评论。

+0

它不工作:( 如果我JS有这样的开始? '<?PHP /** * '排队 如果有帮助,这些都是我接触7场 '

[文字*姓名占位符 “名*”]

[text * lastname placeholder'Last Name *“]

[邮件*电子邮件占位符 “电子邮件地址*”]

[文字*国家占位符 “国家*”]

[提交的 “发送”]

'我还添加了国家行放入脚本'VL.options.form_fields.form_country = $(“#country”)。val(); //捕捉国家(如果适用于您的表格)' –

+0

不,对不起。也许我的帖子不够清楚。 'function js_40916565'块中的代码应该在你的'js/script.js'文件中,除此之外(除非文件已经存在)。 '用jQuery作为一个依赖项'阻塞脚本应该在你的'functions.php'文件中,而on_sent_ok显然应该在WordPress管理员的联系表单7中。 – Jonathan

+0

是的,我已经这样做了,但我使用的是主题选项中的自定义js的Gem主题 - 我在那里添加了'function'位。 'enqueue'位在我的functions.php文件中,文件中已经有'<?php' - 如果我删除它,在网站顶部出现错误。 '联系人7高级设置中的发送好的位。这里的网页,如果有帮助[链接](http://sportshosts.com/home-13-2) –