0

我有几个链接的下拉列表,如果链接的下拉列表为空,我想阻止表单提交更改事件。

<form id="foo">
    <select name="first" on="change:AMP.setState({settings: {first: event.value}}),foo.submit" >
        ...
    </select>

    <select name="second" on="change:AMP.setState({settings: {second: event.value}}),foo.submit" >
        <option value=0> Please select </option>
        ...
    </select>

</form>

如果值合适,我可以检查状态然后提交表单吗?就像是:

<select name="first" on="change:AMP.setState({settings: {first: event.value}}),settings.second ? foo.submit : nothing to do" >

当我选择新值时,第一个下拉列表可以避免表单提交,因为第二个还没有被选中。

尝试通过验证来实现,但它不适用于下拉菜单,输入工作正常。

            <input type="text"
                   id="show-first-on-submit-name"
                   name="name"
                   placeholder="Digit..."
                   required
                   on="change:AMP.setState({settings: {testing: event.value}}),requirements-form.submit"
                   pattern="^([1-9][0-9]*)$">
            <span visible-when-invalid="valueMissing"
                  validation-for="show-first-on-submit-name"></span>
            <span visible-when-invalid="patternMismatch"
                  validation-for="show-first-on-submit-name">
              Please enter not '0'
            </span>

            <select name="purpose"
                    id="purpose"
                    pattern="^([1-9][0-9]*)$"
                    required
                    on="change:AMP.setState({settings: {purpose: event.value}}),requirements-form.submit">
                <option value="0">Please select</option>
                <option value="1">One</option>
            </select>
            <span visible-when-invalid="valueMissing"
                  validation-for="purpose">missing</span>
            <span visible-when-invalid="patternMismatch"
                  validation-for="purpose">wrong</span>
4

1 回答 1

0

通过将输入状态转换为amp-state对象并在发生更改事件时检查它们来验证表单有点迂回。

在我看来,使用HTML5 表单验证 API(AMP 也使用它)来处理输入验证工作会更容易。您需要做的就是向元素添加required属性并删除验证逻辑。<select>onChange

这是一个简单的例子:

<!doctype html>
<html ⚡&gt;
<head>
  <meta charset="utf-8">
  <title>My AMP Page</title>
  <link rel="canonical" href="self.html" />
  <meta name="viewport" content="width=device-width,minimum-scale=1">
  <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
  <style amp-custom>
    h1 {
      margin: 1rem;
    }
  </style>
</head>
<body>
  <form target="_blank" action="/">
    <select name="tier1" required>
      <option value="">Please select</option>
      <option value="opt1">tier1 - opt1</option>
      <option value="opt2">tier1 - opt2</option>
    </select>
    <select name="tier2" required>
      <option value="">Please select</option>
      <option value="opt1">tier2 - opt1</option>
      <option value="opt2">tier2 - opt2</option>
    </select>
    <input type="submit" value="submit" />
  </form>
</body>
    </html>

示例截图:

在此处输入图像描述

于 2018-05-26T15:53:17.960 回答