Dig*_*ent 6 javascript forms validation contact-form-7
在我的联系表格7中,我有两个单选按钮,根据用户的选择显示和隐藏联系表单中的字段.

当您单击"手机"单选按钮时,脚本(JS不是jQuery)会确保隐藏电子邮件字段,并且仅显示电话字段.单击电子邮件单选按钮时,将显示电子邮件字段,并隐藏电话字段.那部分正如我希望的那样工作.
我遇到的问题是我无法弄清楚如何阻止隐藏字段被Contact Form 7验证.例如,如果客户想要输入他们的电话号码而不是他们的电子邮件,插件仍然会给出他们尝试提交时出错,因为电子邮件字段未填写.
这是代码 -
JS:
window.onload=radioCheck;
function radioCheck() {
    if (document.getElementById('pcmPhone').checked) {
        document.getElementById('client-phone').style.display = 'block';
        document.getElementById('client-email').style.display = 'none';
        document.getElementById('phone-input').setAttribute("aria-required", "true");
        document.getElementById('email-input').setAttribute("aria-required", "false");
    } else {
        document.getElementById('client-phone').style.display = 'none';
        document.getElementById('client-email').style.display = 'block';
        document.getElementById('phone-input').setAttribute("aria-required", "false");
        document.getElementById('email-input').setAttribute("aria-required", "true");
    }
}
HTML(有一些联系表格7短代码):
<div class="formFieldWrap">
Name:<br />
[text* client-name]
</div>
<div class="contactPref">
How would you like us to respond?
<br/>
<span class="wpcf7-form-control-wrap cpcm">
<span class="wpcf7-form-control wpcf7-radio" id="cpm">
<span class="wpcf7-list-item">
<input type="radio" id="pcmPhone" id="phone-input" name="cpcm" value="Phone Call" checked="checked" onclick="radioCheck();"/> 
<span class="wpcf7-list-item-label">Phone Call</span>
</span>
<span class="wpcf7-list-item">
<input type="radio" id="pcmEmail" id="email-input" name="cpcm" value="E-mail" onclick="radioCheck();"/> 
<span class="wpcf7-list-item-label">E-mail</span>
</span>
</span>
</span>
</div>
<div class="formFieldWrap" id="client-phone">
Phone:<br/>
[tel* client-phone]
</div>
<div class="formFieldWrap" id="client-email">
E-mail:<br />
[email* client-email]
</div>
<div class="formFieldWrap">
Message:<br />
[textarea client-message]
</div>
[submit id:contactSub "Send"]
我已经尝试更改了aria-required属性,你可以在javascript中看到但我遇到了两个问题 - 1)我用来改变JS属性的方法不起作用.属性保持设置为true.2)当我在我的firebug中手动将它们更改为false时,它们仍以某种方式验证.
所以我的问题是,如何隐藏表单字段?
我也刚刚遇到这个问题。我就是这样解决的。
我正在使用 WPCF7 的过滤器之一在验证发布的数据之前对其进行更改:
function alter_wpcf7_posted_data( $data ) {
    if($_POST['cpcm'] == "E-mail") {
        $_POST['tel'] = "Contact by email";
    }
    if($_POST['cpcm'] == "Phone Call") {
        $_POST['tel'] = "Contact by phone";
    }
    return $data;
}
add_filter("wpcf7_posted_data", "alter_wpcf7_posted_data");
这样,WPCF7 的验证阶段就会认为隐藏字段实际上已被填充。
注意:我没有具体测试上面的代码,但你应该明白了:)
编辑:上面的代码位于主题的functions.php文件中
| 归档时间: | 
 | 
| 查看次数: | 12480 次 | 
| 最近记录: |