WordPress:在jQuery按钮上,单击“联系表单7”菜单中的预选选项

时间:2020-02-18 15:23:58

标签: contact-form contact-form-7

我有一个Wordpress网站,该页面上有很多项目。当用户单击所需项目时,页面将打开。在此页面上有一个按钮“联系”。我要实现的事情是,当用户单击联系人按钮时,将打开带有表单的下一页,并且根据该项目页面上的电子邮件预先选择了选择菜单。有人可以帮我吗?

这是单个帖子页面的某些部分,单击该按钮可以转到表单页面:

<button class="btn">
 <span>Contact</span>
</button>

此页面上有很多信息,其中还提到此页面上此特定项目的电子邮件地址:

<p class="email">Email: <a href="mailto:<?php the_field('contact_email'); ?>">
 <?php the_field('contact_email'); ?>
</a></p>

这是我的联系表7代码:

<div class="contact-form">
<div class="form-group">
  [text* your-name class:form-control placeholder "Name"] 
</div>
<div class="form-group">
  [text* your-name class:form-control placeholder "Company"] 
</div>
<div class="form-group">
  [email* your-email class:form-control placeholder "Email"] 
</div>
<div class="form-group">
  [select* menu-105 class:form-control "Office 1|email1@example.com" "Office 2|email2@example.com" "Office 3|email3@example.com" "Office 4|email4@example.com"]
</div>
<div class="form-group">
  [text your-subject class:form-control placeholder "Subject"] 
</div>
<div class="form-group">
  [textarea your-message x3 class:form-control placeholder "Message"] 
</div>
<div class="form-group form-check">
 [acceptance acceptance-955 optional] By ticking this box you agree to this information here in <a href="https://www.google.com" target="_blank">privacy policy</a>.[/acceptance]
</div>
<div class="btn-container-right">
<div class="btn-wrapper">
  [submit "Send" class:btn-sm]
</div>
</div>
</div>

我要实现的目的是,当用户单击联系人按钮时,打开带有表单的下一页,并根据此项目页面上的电子邮件预先选择选择菜单。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

好的...所以,首先,您想通过按钮将URL参数传递给联系表单。

因此,您更新后的链接是,(您将空白位置编码为%20)

yourdomain.com/contact-form/?email=office%201

然后将该脚本添加到您的主题.js文件中或任何可能的地方。

jQuery(document).ready(function($){
    $.urlParam = function (name) {
        var results = new RegExp('[\?&]' + name + '=([^&#]*)')
                          .exec(window.location.search);

        return (results !== null) ? results[1] || 0 : false;
    }
    // Since above function returns false if blank, check if email $_GET param is entered
    if ( false !== $.urlParam('email')){
        $('select[name="your-email"]').val(decodeURIComponent($.urlParam('email')))
            .change(); // set select name to the name of your form field in CF7
    }
});

decodeURIComponent仅当您使用URL编码参数时...这是因为在选项“ Office 1”,“ Office 2”中有空格

将[name =]设置为您的CF7字段的名称。

相关问题