简体   繁体   English

AMP:在选择框中预先选择的值

[英]AMP: pre selected value in select box

What is the solution to have a pre selected value in the sample below? 在下面的示例中具有预选值的解决方案是什么? Let's say that the third option should be selected by default when data is retrieved from JSON and the select box is shown (without user interaction). 假设当从JSON检索数据并显示选择框时(没有用户交互),默认情况下应选择第三个选项。

<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
<select>
<amp-list width="auto" height="100" layout="fixed-height" src="https://ampproject-b5f4c.firebaseapp.com/examples/data/amp-list-urls.json">
<template type="amp-mustache">
    <option value="{{url}}">{{title}}</option>
</template>
</amp-list>
</select>

The simplest way is adding the selected attribute in the option tags to make it the default. 最简单的方法是将selected属性添加到选项标签中,使其成为默认属性。

<option selected value="{{url}}">{{title}}</option>

If you want to explore other options, this SO post mentioned binding. 如果您想探索其他选项,那么这篇SO文章提到了绑定。 amp-bind adds custom interactivity with data binding and expressions. amp-bind通过数据绑定和表达式添加自定义交互性。

There is a solution with "mustache inverted sections", but it does not work in my case due to other factors involved. 有一个“胡子倒置部分”解决方案,但由于涉及其他因素,因此在我的情况下不起作用。

I got it thanks to [Cathy Zhu][1], on [AMP Issues support page][2]. 我感谢[AMP Issues支持页面] [2]上的[Cathy Zhu] [1]。

I will provide it, in case it would be helpful for someone: 我将提供它,以防对某人有所帮助:

<amp-selector>
<amp-list width="auto" height="200"
  layout="fixed-height"
  src="//amp-list-selector.glitch.me/api/amp-list-data.json">
  <template type="amp-mustache">
    {{#selected}}
    // content displayed if selected is true
    <div class="story-entry"  selected>
      <amp-img src="{{imageUrl}}" width="100" height="100"></amp-img>
    </div>
    {{/selected}}
    {{^selected}}
     // content displayed if selected is false
    <div class="story-entry">
      <amp-img src="{{imageUrl}}" width="100" height="100"></amp-img>
    </div>
    {{/selected}}
  </template>
</amp-list>

for the sample data: 对于样本数据:

{

"items": [ { "title": "Cat", "imageUrl": " https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w400-h300-no-n ", "selected": true }, “ items”:[{“ title”:“ Cat”,“ imageUrl”:“ https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w400-h300-no-n ”,“ selected”:true},

{
  "title": "Dog",
  "imageUrl": "https://lh3.googleusercontent.com/5rcQ32ml8E5ONp9f9-Rf78IofLb9QjS5_0mqsY1zEFc=w400-h300-no-n",
  "selected": false
},
{
  "title": "Mouse",
  "imageUrl": "https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w400-h300-no-n",
  "selected": false
},

{
  "title": "Fish",
  "imageUrl": "https://lh3.googleusercontent.com/5rcQ32ml8E5ONp9f9-Rf78IofLb9QjS5_0mqsY1zEFc=w400-h300-no-n",
  "selected": false
}

] } ]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM