Weebly动态添加的表单元素如何在提交邮件中显示?
解决Weebly动态添加表单元素不随邮件提交的问题
问题核心是你动态创建的输入框缺少name属性——Weebly的表单提交系统只会收集带有name属性的表单元素值,你的代码已经确保元素处于表单标签内部,只需补充name属性即可,同时可以优化选项切换时的字段清理逻辑。
修改后的代码如下:
<script> var inputField = document.getElementById("input-167878397137819290"); inputField.addEventListener('change', addInput); function addInput () { var existingInput = document.getElementById("new-added-input-dt"); // 切换选项时移除已存在的输入框和对应标签 if (existingInput) { existingInput.previousElementSibling.remove(); existingInput.remove(); } var inputValue = inputField.value; if (inputValue === 'English to Spanish' || inputValue === 'English into Spanish') { var newInput = document.createElement('input'); newInput.id = "new-added-input-dt"; newInput.className = "wsite-form-input wsite-input wsite-input-width-370px"; // 核心:添加name属性,Weebly靠此字段识别并提交内容 newInput.name = "input-spanish-country"; inputField.insertAdjacentElement("afterend", newInput); var label = document.createElement('label'); label.for = "new-added-input-dt"; label.className = "wsite-form-label"; label.textContent = "which country will you use the Spanish translation(s) for?"; newInput.insertAdjacentElement("beforebegin", label); } } </script>
关键说明:
- 新增
newInput.name = "input-spanish-country";:自定义唯一的名称即可,只要不与表单内其他字段的name重复,Weebly就能捕获该字段值并包含在提交邮件中。 - 补充选项切换清理逻辑:避免用户切换选择后,无效的输入框仍留在表单中被提交。
内容的提问来源于stack exchange,提问作者DonBergen
相关产品推荐
相关产品推荐

