AMP表单选择如何获取数据属性的值?

时间:2017-10-18 16:13:21

标签: javascript html amp-html

您好!

我使用amp-bind方法。在AMP表格中,我跟踪选择事件的变化。接下来,我更改页面元素的值。

这一切都很好,例如:



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

<form method="post" action-xhr="#">
  <select name="items-select" on="change:AMP.setState({item: event.value})">
    <option value="Milk">Milk</option>
    <option value="Chocolate">Chocolate</option>
    <option value="Cookies">Cookies</option>
  </select>
</form>

<p [text]="'Field Value: ' + item">Field Value: secret</p>
&#13;
&#13;
&#13;

我的问题涉及以下内容:如何读取数据属性的值?

例如:

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

<form method="post" action-xhr="#">
  <select name="items-select" on="change:AMP.setState({item: event.value})">
    <option data-id="1" data-name="item-4453" data-discount="0" value="Milk">Milk</option>
    <option data-id="2" data-name="item-4454" data-discount="1" value="Chocolate">Chocolate</option>
    <option data-id="3" data-name="item-4455" data-discount="1" value="Cookies">Cookies</option>
  </select>
</form>

如何获取所选项目的所有数据的值?

谢谢!

1 个答案:

答案 0 :(得分:4)

        <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>
        <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
        
        <form method="post" action-xhr="#">
          <select name="items-select" on="change:AMP.setState({item: event.value})">
            <option value="1,Milk">Milk</option>
            <option value="2,Chocolate">Chocolate</option>
            <option value="3,Cookies">Cookies</option>
          </select>
        </form>
        
        <p [text]="'Field Value: ' + item.split(',')[0] + ' Field Name: ' + item.split(',')[1]">Field Value: 1 Field Name: Milk</p>

编辑:事实证明,你需要做的事情要比使用<amp-selector>简单得多。目前,没有可能的方法来设置多个属性的数据。但是,可以使用逗号,  或分号;分隔的字符串(适合您)