在放大器上发生事件后选择选项元素

时间:2018-11-08 21:53:21

标签: amp-html

我想在用户更改第一个下拉菜单后自动选择第二个和第三个下拉菜单中的一个。

<body>
  <h1>Hello AMPHTML World!</h1>
  <label for="color">Color</label>
  <select name="color" id="color" on="change:size.focus">
    <option disabled selected></option>
    <option value="blue">blue</option>
    <option value="red">red</option>
    <option value="green">green</option>
  </select>
  <label for="size">Size</label>
  <select name="size" id="size">
    <option disabled selected></option>
    <option value="small">small</option>
    <option value="medium">medium</option>
    <option value="big">big</option>
  </select>
  <label for="material">Material</label>
  <select name="material" id="material">
    <option disabled selected></option>
    <option value="paper">paper</option>
    <option value="plastic">plastic</option>
    <option value="wood">wood</option>
  </select>
</body>

到目前为止,我只找到了动作focus,但是拥有select(value=small)之类的东西会很棒。有什么想法或解决方法吗?谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用amp-bind来实现。当您在第一个下拉列表中选择一个选项时,请设置一个具有所需值的状态。为了进行演示,我创建了一个状态来存储映射,该映射将与所选内容相对应的值设置如下:

  <amp-state id="mapping">
  <script type="application/json">
   {
      "blue" : "small",
      "red" : "medium",
      "green" : "big"
   }
  </script>
  </amp-state>

然后,我们必须在下拉菜单中设置与选择对应的状态,如下所示:

<select name="color" id="color" on="change:AMP.setState({ val : mapping[event.value] })">

现在,如果相应的值设置为selected,则我们必须在第二个下拉列表的每个option中绑定val属性,以将其评估为true:

  <select name="size" id="size">
    <option disabled selected></option>
    <option value="small" [selected]=" val == 'small' ">small</option>
    <option value="medium" [selected]=" val == 'medium' ">medium</option>
    <option value="big" [selected]=" val == 'big' ">big</option>
  </select>

注意:不要忘记添加amp-bind脚本

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

答案 1 :(得分:1)

没有用于选择选项值的操作和事件,您可以为此使用amp-bind

Here is working code

<!doctype html>
<html ⚡>
 <head>
   <meta charset="utf-8">
   <link rel="canonical" href="amp-bind.html">
   <meta name="viewport" content="width=device-width,minimum-scale=1,initial-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-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
  
 </head>
 <body>
 <label for="color">Color</label>
  <select name="color" id="color" on="change:AMP.setState({ optionValue: true })">
    <option disabled selected></option>
    <option value="blue">blue</option>
    <option value="red">red</option>
    <option value="green">green</option>
  </select>
  <label for="size">Size</label>
  <select name="size" id="size">
    <option disabled selected></option>
    <option [selected]="optionValue" value="small">small</option>
    <option value="medium">medium</option>
    <option value="big">big</option>
  </select>
  <label for="material">Material</label>
  <select name="material" id="material">
    <option disabled selected></option>
    <option [selected]="optionValue" value="paper">paper</option>
    <option value="plastic">plastic</option>
    <option value="wood">wood</option>
  </select>
  
 </body>
</html>

另一种通过使用不带amp-bind的html中的技巧来做到这一点的方法

Here is working url

<!doctype html>
<html ⚡>
 <head>
   <meta charset="utf-8">
   <link rel="canonical" href="amp-bind.html">
   <meta name="viewport" content="width=device-width,minimum-scale=1,initial-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-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
  
 </head>
 <body>
 <label for="color">Color</label>
  <select name="color" id="color" on="change:fsize.hide,size.show,fmaterial.hide,material.show">
    <option disabled selected></option>
    <option value="blue">blue</option>
    <option value="red">red</option>
    <option value="green">green</option>
  </select>
  <label for="size">Size</label>
  <select id="fsize">
  <option disabled selected></option>
  </select>
  <select hidden name="size" id="size">
   <option value="small">small</option>
    <option value="medium">medium</option>
    <option value="big">big</option>
  </select>
  <label for="material">Material</label>
   <select id="fmaterial">
  <option disabled selected></option>
  </select>
  <select hidden name="material" id="material">
   <option value="paper">paper</option>
    <option value="plastic">plastic</option>
    <option value="wood">wood</option>
  </select>  
 </body>
</html>