如何在amp-autocomplete选中选项时输入框显示h1值且正常跳转对应url
解决方案
你之前拼接字符串再拆分的方案无法生效,是因为AMP的事件表达式有安全限制,不支持直接调用split这类原生JS方法,你可以通过amp-autocomplete select事件自带的event.item字段实现需求,不需要额外处理字符串:
核心原理
amp-autocomplete触发select事件时,返回的事件对象包含两个常用字段:
event.value:对应模板中data-value属性的值,默认会自动填充到关联的输入框中event.item:你在配置JSON中定义的选中条目完整对象,可直接访问h1、url等所有自定义字段
修改步骤
- 将mustache模板中的
data-value属性设置为{{h1}},保证选中选项后输入框自动填充标题内容 - 将跳转逻辑的取值从
event.value改为event.item.url,直接从完整条目对象中读取跳转地址
关键代码修改片段
<!-- 修改后的模板部分 --> <template type="amp-mustache" id="amp-template-custom"> <div class="slug-item" data-value="{{h1}}"> <span class="autocomplete-link">{{h1}}</span> </div> </template> <!-- 修改后的autocomplete事件配置 --> <amp-autocomplete filter="token-prefix" filter-value="h1" min-characters="2" on="select:AMP.navigateTo(url=event.item.url, target=_blank)"> <input type="search" name="h1" /> <!-- 其余配置不变 --> </amp-autocomplete>
完整可运行代码
<!DOCTYPE html> <html ⚡> <head> <meta charset="utf-8" /> <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> <script async custom-element="amp-autocomplete" src="https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js"></script> <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script> </head> <body> <form class="sample-form" method="post" target="_top" action-xhr="https://amp.dev/documentation/examples/api/echo"> <label> <span>Search for</span> <amp-autocomplete filter="token-prefix" filter-value="h1" min-characters="2" on="select:AMP.navigateTo(url=event.item.url, target=_blank)"> <input type="search" name="h1" /> <script type="application/json"> { "items": [ { "h1": "page1", "url": "https://stackoverflow.com/" }, { "h1": "page2", "url": "https://google.com/" }, { "h1": "page3", "url": "https://amp.dev/" } ] } </script> <template type="amp-mustache" id="amp-template-custom"> <div class="slug-item" data-value="{{h1}}"> <span class="autocomplete-link">{{h1}}</span> </div> </template> </amp-autocomplete> </label> </form> </body> </html>
内容的提问来源于stack exchange,提问作者la_petite_kozel
相关产品推荐
相关产品推荐

