需要 Javascript 才能在联系表中做出具体回复(文本必须等于 "yes")

Need Javascript for Specific Response in Contact Form (text must equal "yes")

我目前有一个联系表单,要求用户输入 'yes' 以达成协议。我知道 HTML 和 CSS,但不知道可能的 Javascript 代码要求用户在文本框中输入 'yes' 并阻止消息发送如果他们要输入 'no' 或 'yes' 以外的任何其他响应,则通过。任何帮助将不胜感激。谢谢!

您可以使用 pattern 属性,表单将负责验证

<form>
  <label for="confirm">Type yes</label>
  <input type="text" id="confirm" name="confirm" placeholder="yes" pattern="^yes$" required>
  <input type="submit" />
</form>

简单的 JavaScript 代码看起来像 将 运行 当用户尝试提交表单时

document.getElementByTagName("form").addEventListener("submit", function(event){
    //get your input by id:
    let input_validate = document.getElementById("your input id");
    if(input_validate.value.toUpperCase() !== "YES"){
        event.preventDefault();
        alert('Please write yes in the input');
        return false;
    }
});

如果没有更多 context/your HTML 以及您希望如何处理不需要的输入(错误消息、阻止表单提交、重定向到新页面,以上所有)。这可以在输入的 onchange 函数中进行,或者在表单提交期间调用的函数中进行。

var response = document.getElementById('[your_inputs_id]').value;
if(response == "yes"){
    //do things, or submit, or do nothing
    return true;
}
else{
   //display error message, don't submit, etc.
   return false;
}