简体   繁体   English

有条件地隐藏或显示输入 - AMP

[英]Conditionally hide or show inputs - AMP

I am trying to build a form using Accelerated Mobile Pages (AMP) and I need to hide or show inputs based on user selection. 我正在尝试使用加速移动页面(AMP)构建表单,我需要根据用户选择隐藏或显示输入。

I have a <select> where users can choose the country: 我有一个<select> ,用户可以选择国家/地区:

<select name="country" id="country" required>
    <option value="UK">United Kingdom</option>
    <option value="ES">Spain</option>
</select>

And if the user chooses United Kingdom I want to hide this inputs: 如果用户选择英国,我想隐藏此输入:

<input type="text" id="idcard" name="idcard">
<input type="text" id="mobile" name="mobile">

I have already tried using the "on" attribute inside the <option> tag: 我已经尝试使用<option>标记内的“on”属性

<option value="UK" on="tap:idcard.hide,mobile.hide">United Kingdom</option>

but it doesn't work and it is only valid on the <select> tag even the documentation says "All elements". 但它不起作用,它只在<select>标签上有效,即使文档说“所有元素”。

I need to use <select> and <option> tags as there are a lot of countries and not just 2, otherwise with a radio input the "on" attribute would work. 我需要使用<select><option>标签,因为有很多国家而不仅仅是2,否则使用无线电输入“on”属性就可以了。

Is there any kind of trigger or event I can use to hide or show inputs based on user selection? 是否有任何类型的触发器或事件可用于隐藏或显示基于用户选择的输入?

Hope anyone can help! 希望有人可以帮忙! Thanks! 谢谢!

You can use the change event on the select element, and test the value that was selected, and based on that value, set an AMP state property visibility to value show or hide like this: 您可以在select元素上使用change事件,并测试所选的值,并根据该值将AMP状态属性visibility为值showhide如下所示:

<select name="country" id="country" required
        on="change:AMP.setState({visibility: event.value=='ES'?'hide':'show'})">
 <option value="UK" >UK</option>
 <option value="ES" >Spain</option>
</select>

Then bind the class of the inputs to the value of visibility: 然后将输入类绑定到visibility的值:

<input type="text" id="idcard" name="idcard" 
       class="show"
       [class]="visibility||'show'">
<input type="text" id="mobile" name="mobile"
       class="show"
       [class]="visibility||'show'">

Then you just need CSS class: 那你只需要CSS类:

<style amp-custom>
  .hide {display: none;}
</style>

You will need to include amp-bind component: 您需要包含amp-bind组件:

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

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

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