【发布时间】:2014-01-22 09:31:57
【问题描述】:
在我的联系表单 7 中,我有两个单选按钮,根据用户所做的选择显示和隐藏联系表单中的字段。
当您单击“电话”单选按钮时,脚本(JS 而不是 jQuery)会确保电子邮件字段被隐藏并且仅显示电话字段。当您单击电子邮件单选按钮时,电子邮件字段会显示,而电话字段会隐藏。该部分的工作方式完全符合我的要求。
我遇到的问题是我无法弄清楚如何阻止隐藏字段被联系表 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 所需的属性,正如您在 javascript 中看到的那样,但我遇到了两个问题 - 1) 我使用 JS 更改这些属性的方法不起作用。属性保持设置为 true。 2) 当我在 firebug 中手动将它们更改为 false 时,它们仍然以某种方式验证。
所以我的问题是,如何在隐藏表单字段时禁用它?
【问题讨论】:
标签: javascript forms validation contact-form-7