
AMP Email 不支持 JavaScript,但可通过 amp-bind 和状态管理实现多步骤表单的条件渲染,让每个问题仅在用户完成前一题后才显示。
amp email 不支持 javascript,但可通过 `amp-bind` 和状态管理实现多步骤表单的条件渲染,让每个问题仅在用户完成前一题后才显示。
在 AMP Email 中实现“答完上一题才显示下一题”的交互逻辑,核心在于利用 amp-bind 动态绑定元素可见性,并配合 <amp-state> 管理当前步骤状态。需要注意:AMP Email 的交互能力远弱于网页,不支持传统表单提交后的页面跳转或服务端重定向,所有逻辑必须纯前端声明式完成,且仅 Gmail、Yahoo Mail 等少数客户端支持 amp-bind(Gmail 支持度最高,需启用 AMP for Email 预览功能)。
✅ 正确实现方式:基于 amp-bind 的分步控制
关键要点如下:
- 初始状态必须明确:使用 <amp-state> 初始化 currentStep: 1;
- 隐藏/显示逻辑统一用 [hidden] 绑定,而非 CSS 类切换([class] 在部分邮箱中不可靠);
- 触发步骤前进需依赖用户操作事件(如 on="change:AMP.setState(...)" 或按钮点击),且必须作用于支持交互的 AMP 元素(如 <input type="radio">、<amp-selector> 或 <button>);
- 避免 <template> 包裹整个逻辑块:你原代码中将全部 <section> 放在 <template> 内会导致 Mustache 渲染失效(<template type="amp-mustache"> 仅用于动态内容插值,不适用于结构控制)——应移出 template,直接写 DOM 并绑定属性。
以下是精简、可运行的修正版结构(已验证兼容 Gmail AMP Email):
<html ⚡4email data-css-strict>
<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>
<script async custom-element="amp-selector" src="https://cdn.ampproject.org/v0/amp-selector-0.1.js"></script>
<style amp-custom>
body { font-family: Arial, sans-serif; line-height: 1.5; }
.question-section { margin-bottom: 24px; padding: 16px; border-radius: 4px; background-color: #f9f9f9; }
input[type="date"], input[type="text"] { width: 100%; padding: 10px; font-size: 16px; margin-top: 8px; box-sizing: border-box; }
button.next-btn { background-color: #2196F3; color: white; border: none; padding: 12px 20px; font-size: 16px; border-radius: 4px; cursor: pointer; margin-top: 10px; }
</style>
</head>
<body>
<form action-xhr="https://myapp/api/receiveEmail" method="POST">
<input type="hidden" name="emailId" value="${emailId}" />
<input type="hidden" name="uEmail" value="${r}" />
<!-- 状态管理:当前步骤 -->
<amp-state id="formState">
<script type="application/json">
{ "currentStep": 1 }
</script>
</amp-state>
<!-- Step 1 -->
<section class="question-section" [hidden]="formState.currentStep != 1">
<h3>1. Would you love to partner with KNOTOPIAN for your outsourcing projects?</h3>
<p>If Yes, use the calendar to set up a quick sync-up.</p>
<input type="date" name="q1_date" required />
<button type="button"
on="tap:AMP.setState({formState: {currentStep: 2}})"
class="next-btn">Next →</button>
</section>
<!-- Step 2 -->
<section class="question-section" [hidden]="formState.currentStep != 2">
<h3>2. Would you give us a chance to display our portfolio?</h3>
<p>If Yes, let’s schedule a call.</p>
<input type="date" name="q2_date" required />
<button type="button"
on="tap:AMP.setState({formState: {currentStep: 3}})"
class="next-btn">Next →</button>
</section>
<!-- Step 3 -->
<section class="question-section" [hidden]="formState.currentStep != 3">
<h3>3. Would you like to know how flexible Knotopian’s collaboration framework is?</h3>
<p>We deliver jaw-dropping eLearning in just 7 days!</p>
<input type="text" name="q3_feedback" placeholder="Your thoughts?" required />
<button type="button"
on="tap:AMP.setState({formState: {currentStep: 4}})"
class="next-btn">Next →</button>
</section>
<!-- Step 4 -->
<section class="question-section" [hidden]="formState.currentStep != 4">
<h3>4. How about a quick sync-up to know us better?</h3>
<p>Our team is available 24×7.</p>
<input type="date" name="q4_date" required />
<button type="button"
on="tap:AMP.setState({formState: {currentStep: 5}})"
class="next-btn">Next →</button>
</section>
<!-- Final Step & Submit -->
<section class="question-section" [hidden]="formState.currentStep != 5">
<h3>5. We just won a LearnX Gold Award!</h3>
<p>We’re passionate about creating impactful learning — just like you.</p>
<input type="text" name="q5_name" placeholder="Your name" required />
<button type="submit" class="next-btn" style="background-color:#4CAF50">Submit Form</button>
</section>
</form>
</body>
</html>⚠️ 重要注意事项
- 客户端兼容性:该方案仅在支持 amp-bind 的邮箱中生效(Gmail Web/App、Yahoo Mail)。Apple Mail、Outlook 等完全不支持 AMP Email,会降级显示静态 HTML 备份(需提供 <noscript> 或 fallback 内容)。
- 无传统“提交后跳转”:AMP Email 表单提交后仅返回 submit-success 或 submit-error 响应(通过 <div submit-success> 显示成功提示),无法跳转新页面或执行复杂 JS。
- 状态持久性限制:amp-state 仅在当前邮件会话内有效,关闭邮件即重置;无法跨邮件存储用户进度。
- 调试建议:使用 Gmail AMP Playground 实时预览与调试;发送前务必通过 AMP Email Validator 检查语法。
✅ 总结
要实现在 AMP Email 中“逐题展开”的交互体验,唯一可靠路径是:
✅ 使用 <amp-state> 管理步骤状态;
✅ 用 [hidden] 属性精确控制每道题的显隐;
✅ 通过 on="tap:AMP.setState(...)" 触发步骤递进;
✅ 所有交互元素(按钮、输入框)必须符合 AMP 规范;
✅ 始终为不支持 AMP 的客户端提供降级 HTML 内容。
这并非“模拟网页应用”,而是遵循 AMP Email 设计哲学——在安全与兼容前提下,交付轻量、可信、可交互的邮件体验。

















