首页 文章

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

提问于
浏览
0

大家好我正在使用Wordpress Contact Form 7插件并尝试实现: Show text field if drop down "Other" is selected

Drop down menu items

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

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

1 回答

  • 4

    对于任何寻求更简单解决方案的人 . 在联系表单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>
    

    希望有所帮助 .

    如果您有兴趣阅读更多内容或扩展相同的单选按钮,我最近发表了一篇文章,其中包含更多代码示例和示例here .

相关问题