AMP
  • 网站

amp-bind-recaptcha

简介

此示例演示如何仅使用 amp-bind 创建 recaptcha 输入。

导入 amp-bind,以便 recaptcha 可以有多种状态

<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>

Recaptcha 输入用于验证 amp-form 的用户

<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>

实现

下面的 amp-state 定义了 recaptcha 方程式可能出现的三种不同状态。

<amp-state id="captcha">
  <script type="application/json">
    {
       "state1": {
             "result": "10",
             "condition": "+",
             "captchaCorrect": "6"

       },
       "state2": {
             "result": "2",
             "condition": "-",
             "captchaCorrect": "6"
       },
       "state3": {
             "result": "8",
             "condition": "*",
             "captchaCorrect": "2"
       }
     }
  </script>
</amp-state>

表单

Recaptcha 强制用户使用 [pattern] 要求提供正确的输入。 [pattern] 会随着状态的更改而动态更新。为了使 recaptcha 在第一次通过时起作用,请在设置 amp-bind 'state' 变量之前禁用输入(即 [disabled]="!state")。作为另一种解决方法,您可以设置默认模式(不带括号),并让 [pattern] 在用户刷新条件时更新。刷新时,'state' 将更新以提供新的方程式。

+ 4 = 10
<form action="https://www.google.com/" method="get" target="_top">
  <input name="s" placeholder="Type Anything" type="text" on="input-debounced:AMP.setState({state: 'state1'})" required>
    <input [disabled]="!state" disabled type="text" name [pattern]="captcha[state].captchaCorrect" title="AMP recaptcha input" required>
    <span [text]="captcha[state].condition">+</span>
    <span>4</span>
    <span>=</span>
    <span [text]="captcha[state].result">10</span>
    <span on="tap:AMP.setState({state: (state == 'state1' ? 'state2' : state == 'state2' ? 'state3': 'state1')})" role="button" tabindex="0"></span>
    <amp-img src="https://fonts.gstatic.com/s/i/materialicons/autorenew/v4/24px.svg" width="24" height="24"></amp-img>
  <input type="submit" value="Submit">
</form>
需要进一步解释?

如果此页面上的解释没有涵盖您的所有问题,请随时与其他 AMP 用户联系,讨论您的确切用例。

转到 Stack Overflow
一个无法解释的功能?

AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的长期参与者,但我们也欢迎您对您特别感兴趣的问题进行一次性贡献。

在 GitHub 上编辑示例