在联系表格 7 中选择下拉列表后显示输入字段

Bil*_*eer 1 html javascript wordpress jquery contact-form-7

嗨,伙计们,我正在使用 Wordpress Contact Form 7 插件并试图实现:如果选择下拉“其他”,则显示文本字段

下拉菜单项

dropdown在联系表格 7 中添加为 id后,我现在通过 Wordpress 页面中的 Visual composer 使用以下原始 JS :

var x = document.getElementById("dropdown");
if(x.value = "Other") {
 alert('Enter your js here!');
}
Run Code Online (Sandbox Code Playgroud)

Aru*_*Lal 5

对于任何寻求更简单解决方案的人。在联系表 7 中,您可以简单地添加内联 JavaScript。

只要您不在脚本中添加空行,添加到表单构建器的 JavaScript 就会呈现在前端。

这是来自 CF7 表单编辑器的副本:

<label> Your Name (required)
[text* your-name] </label>

<label> Your Email (required)
[email* your-email] </label>

<label> Your Favorite Color
[select drop-down-menu id:FavoriteColorDropDown "Pink" "Red" "Purple" "Other"] </label>

<label id="EnterFavoriteColorLabel"> Please Specify Your Favorite Color
[text favorite-color] </label>

[submit "Send"]

<script language="javascript" type="text/javascript">
// Hide the favorite-color text field by default
document.getElementById("EnterFavoriteColorLabel").style.display = 'none';
// On every 'Change' of the drop down with the ID "FavoriteColorDropDown" call the displayTextField function
document.getElementById("FavoriteColorDropDown").addEventListener("change", displayTextField);
  function displayTextField() {
    // Get the value of the selected drop down
    var dropDownText = document.getElementById("FavoriteColorDropDown").value;
    // If selected text matches 'Other', display the text field.
    if (dropDownText == "Other") {
      document.getElementById("EnterFavoriteColorLabel").style.display = 'block';
    } else {
      document.getElementById("EnterFavoriteColorLabel").style.display = 'none';
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

希望有帮助。

如果你有兴趣阅读更多或延长单选按钮一样,我最近发表的一篇文章有更多的代码样本和范例在这里