首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我找到了一种不用后端使用Formspree从网站直接发送电子邮件的方法。但是,我不知道如何自定义提交的消息

我找到了一种不用后端使用Formspree从网站直接发送电子邮件的方法。但是,我不知道如何自定义提交的消息
EN

Stack Overflow用户
提问于 2021-12-12 19:25:32
回答 1查看 91关注 0票数 0

我目前正在构建一个表单,该表单使用formspree.io api向我的电子邮件发送消息,但我不理解所写的js。实际上,我想让它在发送表单时显示#状态错误或成功消息。表单工作,但消息没有显示。

代码语言:javascript
运行
复制
var form = document.getElementById("my-form");

async function handleSubmit(event) {
    event.preventDefault();
    var status = document.getElementById("status");
    var data = new FormData(event.target);
    fetch(event.target.action, {
    method: form.method,
    body: data,
    headers: {
        'Accept': 'application/json'
    }
    }).then(response => {
    status.innerHTML = "Thank you, received submission!";
    form.reset()
    }).catch(error => {
    status.innerHTML = "Oops! There was a problem submitting your form"
    });
}
form.addEventListener("submit", handleSubmit)
代码语言:javascript
运行
复制
.contact-form{
  width: 500px;
  height: 600px;
  color: white;
  box-shadow: var(--box-shadow);
  border-radius: var(--border-radius);
  margin-bottom: 100px;
  display: flex;
  flex-direction: column;
  
}
form{
  margin: 35px;
  margin-left: 50px;
}

.input-field{
  width: 400px;
  height: 40px;
  margin-top: 40px;
  padding-left: 10px;
  padding-right: 10px;
  border:  1px solid #c4c4c4;
  border-radius: 9px;
  outline: none;
}

.text-area{
  height: 150px;
}
.submit{
  border-radius: 40px;
 border: none;
 background-color: var(--primary-color);
  color: #FFF;
  width: 250px;
  height: 50px;
  margin:  20px 80px;
  cursor: pointer;
  font-weight: bold;
  transition: 1s ease;

}
.submit:hover{
  background-color: #FFF;
  color: var(--primary-color);
  box-shadow: var(--box-shadow);
}
#status{
  width: 300px;
  max-width: 300px;
  height: 50px;
  max-height: 90px;
  margin: -50px 100px;
  text-align: center;
  font-weight: bold;

}
#status.success{
  color: var(--primary-color);
  animation: status 4s ease forwards;
}

#status.error{
  color: var(--primary-color);
  animation: status 4s ease forwards;
}

@keyframes status{
  0%{
    opacity: 1;
    pointer-events: all; 
  }
  90%{
    opacity: 1;
    pointer-events: all; 
  }
  100%{
    opacity: 0;
    pointer-events: none; 
  }
}
代码语言:javascript
运行
复制
            <div class="contact-form mx-auto">
              <form action="https://formspree.io/f/xeqnqkve" id="my-form" method="POST">
                <label for="Name">
                  <input type="text" class="input-field" id="yourname" name="Name" placeholder="Your Name">
                </label>
                <label for="Email">
                  <input type="text" class="input-field" id="youremail" name="Email" placeholder="Your Email">
                </label>
                <label for="Subject">
                  <input type="text" class="input-field" id="subject" name="Subject" placeholder="Subject">
                </label>
                <label for="Message">
                  <textarea type="text" class="input-field text-area" placeholder="Message" name="message" id="message"></textarea>
                </label>
                <button type="submit" class="submit">Send Message</button>
              </form>
              <div id="status" class="success">Thank you!</div>
              
                
                
            </div>

站点中未更改的js代码如下所示。

代码语言:javascript
运行
复制
<script>
    var form = document.getElementById("my-form");
    
    async function handleSubmit(event) {
      event.preventDefault();
      var status = document.getElementById("my-form-status");
      var data = new FormData(event.target);
      fetch(event.target.action, {
        method: form.method,
        body: data,
        headers: {
            'Accept': 'application/json'
        }
      }).then(response => {
        status.innerHTML = "Thanks for your submission!";
        form.reset()
      }).catch(error => {
        status.innerHTML = "Oops! There was a problem submitting your form"
      });
    }
    form.addEventListener("submit", handleSubmit)
</script>

我想要“谢谢,收到意见书!”单击submit按钮后显示在按钮下面,如下图所示。你的帮助将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2021-12-12 20:02:27

您的html代码应该修改:我将状态id的div更改为“提交”按钮的上方。

代码语言:javascript
运行
复制
<div class="contact-form mx-auto">
          <form action="https://formspree.io/f/xeqnqkve" id="my-form" method="POST">
            <label for="Name">
              <input type="text" class="input-field" id="yourname" name="Name" placeholder="Your Name">
            </label>
            <label for="Email">
              <input type="text" class="input-field" id="youremail" name="Email" placeholder="Your Email">
            </label>
            <label for="Subject">
              <input type="text" class="input-field" id="subject" name="Subject" placeholder="Subject">
            </label>
            <label for="Message">
              <textarea type="text" class="input-field text-area" placeholder="Message" name="message" id="message"></textarea>
            </label>
             <div id="status" class="success">Thank you!</div>
            <button type="submit" class="submit">Send Message</button>
          </form>
          
          
            
            
        </div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70326939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档