You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React应用中AMP Email条件显示问题失效,求解决方案

AMP邮件分步表单修复方案

你的代码存在几个核心问题导致在Gmail和Yahoo中无法正常工作:

  1. 错误使用amp-mustache模板:该模板需配合amp-list或触发渲染的动作才能生效,直接放置不会渲染内容。
  2. 逻辑不符合需求:你用amp-selector让用户手动选择问题,但需求是完成上一个问题后自动显示下一个,两者逻辑不匹配。
  3. [hidden]属性兼容性问题:部分邮件客户端对AMP的[hidden]动态绑定支持不佳,建议用类切换控制显示。

以下是修复后的完整代码,完全适配Gmail和Yahoo的AMP邮件规范:

<!DOCTYPE html>
<html ⚡4email>
<head>
  <meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <style amp4email-boilerplate>body{visibility:hidden}</style>
  <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
  <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
  <style amp-custom>
    body {
      font-family: Arial, sans-serif;
      max-width: 600px;
      margin: 0 auto;
      padding: 20px;
    }
    .question-section {
      margin-bottom: 25px;
    }
    .hide {
      display: none !important;
    }
    .show {
      display: block;
    }
    label {
      font-size: 18px;
      margin-bottom: 10px;
      color: #333;
      display: block;
    }
    h5 {
      font-size: 14px;
      color: #555;
      margin: 10px 0 15px;
      font-weight: normal;
    }
    input[type="date"] {
      width: 100%;
      padding: 10px;
      font-size: 16px;
      box-sizing: border-box;
      border: 1px solid #ddd;
      border-radius: 4px;
    }
    .submit-btn {
      background-color: #4caf50;
      color: #fff;
      border: none;
      padding: 10px 20px;
      font-size: 16px;
      cursor: pointer;
      border-radius: 4px;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <div>
    <form action-xhr="https://myapp/api/receiveEmail" method="POST">
      <input type="hidden" name="emailId" id="emailId" value="${emailId?.toString()}" />
      <input type="hidden" name="uEmail" id="uEmail" value="${r?.toString()}" />

      <amp-state id="formState">
        <script type="application/json">
          {
            "currentQuestion": 1
          }
        </script>
      </amp-state>

      <!-- 问题1 -->
      <section class="question-section [class]="formState.currentQuestion === 1 ? 'show' : 'hide'">
        <label>1. 你是否愿意与KNOTOPIAN合作外包项目?</label>
        <h5>如果愿意,请选择方便的时间进行简短沟通,进一步讨论细节。</h5>
        <input type="date" name="question1" required
          on="change:AMP.setState({formState: {currentQuestion: 2}})" />
      </section>

      <!-- 问题2 -->
      <section class="question-section [class]="formState.currentQuestion === 2 ? 'show' : 'hide'">
        <label>2. 相信我们,你已经错过了很多机会!能否给我们一个展示作品集的机会?</label>
        <h5>如果愿意,请选择方便的时间进行简短沟通,进一步讨论细节。</h5>
        <input type="date" name="question2" required
          on="change:AMP.setState({formState: {currentQuestion: 3}})" />
      </section>

      <!-- 问题3 -->
      <section class="question-section [class]="formState.currentQuestion === 3 ? 'show' : 'hide'">
        <label>3. 我们仍希望让你了解我们能提供的帮助!毕竟我们有7天内打造惊艳电子学习内容的记录!你是否想了解Knotopian灵活的合作框架?</label>
        <h5>如果愿意,请选择方便的时间进行简短沟通,进一步讨论细节。</h5>
        <input type="date" name="question3" required
          on="change:AMP.setState({formState: {currentQuestion: 4}})" />
      </section>

      <!-- 问题4 -->
      <section class="question-section [class]="formState.currentQuestion === 4 ? 'show' : 'hide'">
        <label>4. 只需几分钟,就能为你节省大量成本!你不会后悔的。顺便提一下,我们团队24/7为你提供支持。先不谈合作,能否抽时间简单认识一下我们?</label>
        <h5>如果愿意,请选择方便的时间进行简短沟通,进一步讨论细节。</h5>
        <input type="date" name="question4" required
          on="change:AMP.setState({formState: {currentQuestion: 5}})" />
      </section>

      <!-- 问题5 -->
      <section class="question-section [class]="formState.currentQuestion === 5 ? 'show' : 'hide'">
        <label>5. 我们刚刚凭借创新学习方案获得LearnX金奖!只是想让你知道!当然,我们仍希望与你会面。毕竟我们有着共同的目标——打造有影响力的学习内容。</label>
        <h5>如果愿意,请选择方便的时间进行简短沟通,进一步讨论细节。</h5>
        <input type="date" name="question5" required />
      </section>

      <!-- 提交按钮 -->
      <button class="submit-btn [class]="formState.currentQuestion === 5 ? 'show' : 'hide'" type="submit">
        提交
      </button>
    </form>
  </div>
</body>
</html>

关键修复点说明

  • 移除了无用的amp-mustache和amp-selector,直接用amp-bind控制每个问题的显示状态。
  • 给每个问题的日期输入框添加change事件,用户选择日期后自动切换到下一个问题,符合"完成上一个显示下一个"的需求。
  • 用[class]切换show/hide类替代[hidden],解决邮件客户端兼容性问题。
  • 添加了required属性确保用户必须完成当前问题才能进入下一个(可根据需求调整)。
  • 优化了样式,提升邮件在客户端的显示效果。

内容的提问来源于stack exchange,提问作者Nouman Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 10:37:03