选中/取消选中 AMP 上未正确更新的无线电输入字段
Check / uncheck radio input field not updated properly on AMP
我开发了一个 AMP 网站,其中有三个单选按钮。
- 显示全部
- 只显示最喜欢的
- 全部清除
当我点击清除所有按钮时,它会显示一个灯箱。在那个灯箱上我有两个按钮。
- 还行
- 取消
当用户从灯箱中单击取消时,将选中显示所有单选输入。我已经通过 amp-state 变量更改实现了它。这是我第一次点击取消时,它在显示所有无线电输入中正确检查,但之后如果我点击清除无线电输入,然后再次取消按钮,它不会再次检查显示所有无线电输入。
这里我在codepen中添加了示例代码
示例代码 link:https://codepen.io/sayalok/pen/VdEagq
<!DOCTYPE html>
<html amp lang="en">
<head>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Favourites </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1">
<link rel="manifest" href="/manifest.json">
<link rel="canonical" href="https://ampbyexample.com/introduction/hello_world/">
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<script async custom-element="amp-lightbox" src="https://cdn.ampproject.org/v0/amp-lightbox-0.1.js"></script>
<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>
<style amp-custom="">
amp-lightbox {
background: rgba(0, 0, 0, .75);
}
amp-lightbox .align-content-center {
height: 100%;
}
.fav-lightbox{
background: #fff;
width: 70%;
position: relative;
top: 45%;
left: 0;
right: 0;
margin: 0 auto;
padding:15px;
border-radius: 10px;
padding-bottom: 35px;
}
.fav-lightbox h4{
color: #000;
margin:0;
font-weight: 600;
margin-bottom: 10px;
}
.fav-pop-title{
overflow: auto;
}
.lightbox-btn-grup{
float: right;
}
.lightbox-btn-grup input[type="submit"], .cancel-btn {
background: none;
border: none;
color:#3A98FF;
font-size: 16px;
}
.lightbox-btn-grup .favOkBtn{
font-weight: 700;
}
.cancel-btn{
text-decoration: none;
}
</style>
</head>
<body>
<amp-state id="partnerLogo">
<script type="application/json">
{
"chkRadio" : "show_all"
}
</script>
</amp-state>
<div class="favorite-page-wrapper static-page-margin">
<div class="favorite-content-filter">
<label>
<input name="filter" value="show_all" type="radio" role="button" tabindex="0"
on="change:AMP.setState({partnerLogo:{chkRadio : 'show_all'}})" checked
[checked]="partnerLogo.chkRadio == 'show_all' ? checked : '' ">
show all
</label>
<label>
<input name="filter" value="show_favorite" type="radio" role="button" tabindex="0"
on="change:AMP.setState({partnerLogo:{chkRadio : 'show_all'}})"
[checked]="partnerLogo.chkRadio == 'show_fav' ? checked : '' ">
show only favorites
</label>
<label>
<amp-lightbox id="favorite-lightbox" layout="nodisplay">
<div class="fav-lightbox" role="button" tabindex="0">
<div class="fav-pop-title">
<h4>Are you sure you want to clear entire list?</h4>
</div>
<div class="lightbox-btn-grup">
<input type="submit" value="Cancel" role="button" tabindex="0"
on="tap:favorite-lightbox.close ,AMP.setState({partnerLogo:{chkRadio : 'show_all'}})"/>
<input type="submit" value="OK" role="button" tabindex="0" class="favOkBtn"
on="tap:favorite-lightbox.close">
</div>
</div>
</amp-lightbox>
<input
name="filter" type="radio" role="button"
tabindex="0" on="change:favorite-lightbox"
[checked]="partnerLogo.chkRadio != show_all ? checked : '' "> Clear All
</label>
</div>
</div>
</body>
</html>
有什么建议吗?
好吧,我终于从 amp-team 找到了解决这个问题的方法。我发布了那个答案,这样如果有人遇到同样的问题,他们可以轻松解决
<div class="favorite-content-filter">
<label>
<input name="filter" value="show_all" type="radio" role="button" tabindex="0"
on="change:AMP.setState({partnerLogo:{chkRadio : 'show_all'}})" checked
[checked]="partnerLogo.chkRadio == 'show_all' ? checked : '' ">
show all
</label>
<label>
<input name="filter" value="show_favorite" type="radio" role="button" tabindex="0"
on="change:AMP.setState({partnerLogo:{chkRadio : 'show_all'}})"
[checked]="partnerLogo.chkRadio == 'show_fav' ? checked : '' ">
show only favorites
</label>
<label>
<amp-lightbox id="favorite-lightbox" layout="nodisplay">
<div class="fav-lightbox" role="button" tabindex="0">
<div class="fav-pop-title">
<h4>Are you sure you want to clear entire list?</h4>
</div>
<div class="lightbox-btn-grup">
<input type="submit" value="Cancel" role="button" tabindex="0"
on="tap:favorite-lightbox.close ,AMP.setState({partnerLogo:{chkRadio : 'show_all'}})"/>
<input type="submit" value="OK" role="button" tabindex="0" class="favOkBtn"
on="tap:favorite-lightbox.close">
</div>
</div>
</amp-lightbox>
<input
name="filter" type="radio" role="button"
tabindex="0" on="change:favorite-lightbox, AMP.setState({partnerLogo:{chkRadio : ''}})"
[checked]="partnerLogo.chkRadio != show_all ? checked : '' "> Clear All
</label>
</div>
我只需要在灯箱打开时将 partnerLogo.chkRadio 设置为其他内容。否则,状态将与所选电台不同步。
我开发了一个 AMP 网站,其中有三个单选按钮。
- 显示全部
- 只显示最喜欢的
- 全部清除
当我点击清除所有按钮时,它会显示一个灯箱。在那个灯箱上我有两个按钮。
- 还行
- 取消
当用户从灯箱中单击取消时,将选中显示所有单选输入。我已经通过 amp-state 变量更改实现了它。这是我第一次点击取消时,它在显示所有无线电输入中正确检查,但之后如果我点击清除无线电输入,然后再次取消按钮,它不会再次检查显示所有无线电输入。
这里我在codepen中添加了示例代码
示例代码 link:https://codepen.io/sayalok/pen/VdEagq
<!DOCTYPE html>
<html amp lang="en">
<head>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Favourites </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1">
<link rel="manifest" href="/manifest.json">
<link rel="canonical" href="https://ampbyexample.com/introduction/hello_world/">
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<script async custom-element="amp-lightbox" src="https://cdn.ampproject.org/v0/amp-lightbox-0.1.js"></script>
<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>
<style amp-custom="">
amp-lightbox {
background: rgba(0, 0, 0, .75);
}
amp-lightbox .align-content-center {
height: 100%;
}
.fav-lightbox{
background: #fff;
width: 70%;
position: relative;
top: 45%;
left: 0;
right: 0;
margin: 0 auto;
padding:15px;
border-radius: 10px;
padding-bottom: 35px;
}
.fav-lightbox h4{
color: #000;
margin:0;
font-weight: 600;
margin-bottom: 10px;
}
.fav-pop-title{
overflow: auto;
}
.lightbox-btn-grup{
float: right;
}
.lightbox-btn-grup input[type="submit"], .cancel-btn {
background: none;
border: none;
color:#3A98FF;
font-size: 16px;
}
.lightbox-btn-grup .favOkBtn{
font-weight: 700;
}
.cancel-btn{
text-decoration: none;
}
</style>
</head>
<body>
<amp-state id="partnerLogo">
<script type="application/json">
{
"chkRadio" : "show_all"
}
</script>
</amp-state>
<div class="favorite-page-wrapper static-page-margin">
<div class="favorite-content-filter">
<label>
<input name="filter" value="show_all" type="radio" role="button" tabindex="0"
on="change:AMP.setState({partnerLogo:{chkRadio : 'show_all'}})" checked
[checked]="partnerLogo.chkRadio == 'show_all' ? checked : '' ">
show all
</label>
<label>
<input name="filter" value="show_favorite" type="radio" role="button" tabindex="0"
on="change:AMP.setState({partnerLogo:{chkRadio : 'show_all'}})"
[checked]="partnerLogo.chkRadio == 'show_fav' ? checked : '' ">
show only favorites
</label>
<label>
<amp-lightbox id="favorite-lightbox" layout="nodisplay">
<div class="fav-lightbox" role="button" tabindex="0">
<div class="fav-pop-title">
<h4>Are you sure you want to clear entire list?</h4>
</div>
<div class="lightbox-btn-grup">
<input type="submit" value="Cancel" role="button" tabindex="0"
on="tap:favorite-lightbox.close ,AMP.setState({partnerLogo:{chkRadio : 'show_all'}})"/>
<input type="submit" value="OK" role="button" tabindex="0" class="favOkBtn"
on="tap:favorite-lightbox.close">
</div>
</div>
</amp-lightbox>
<input
name="filter" type="radio" role="button"
tabindex="0" on="change:favorite-lightbox"
[checked]="partnerLogo.chkRadio != show_all ? checked : '' "> Clear All
</label>
</div>
</div>
</body>
</html>
有什么建议吗?
好吧,我终于从 amp-team 找到了解决这个问题的方法。我发布了那个答案,这样如果有人遇到同样的问题,他们可以轻松解决
<div class="favorite-content-filter">
<label>
<input name="filter" value="show_all" type="radio" role="button" tabindex="0"
on="change:AMP.setState({partnerLogo:{chkRadio : 'show_all'}})" checked
[checked]="partnerLogo.chkRadio == 'show_all' ? checked : '' ">
show all
</label>
<label>
<input name="filter" value="show_favorite" type="radio" role="button" tabindex="0"
on="change:AMP.setState({partnerLogo:{chkRadio : 'show_all'}})"
[checked]="partnerLogo.chkRadio == 'show_fav' ? checked : '' ">
show only favorites
</label>
<label>
<amp-lightbox id="favorite-lightbox" layout="nodisplay">
<div class="fav-lightbox" role="button" tabindex="0">
<div class="fav-pop-title">
<h4>Are you sure you want to clear entire list?</h4>
</div>
<div class="lightbox-btn-grup">
<input type="submit" value="Cancel" role="button" tabindex="0"
on="tap:favorite-lightbox.close ,AMP.setState({partnerLogo:{chkRadio : 'show_all'}})"/>
<input type="submit" value="OK" role="button" tabindex="0" class="favOkBtn"
on="tap:favorite-lightbox.close">
</div>
</div>
</amp-lightbox>
<input
name="filter" type="radio" role="button"
tabindex="0" on="change:favorite-lightbox, AMP.setState({partnerLogo:{chkRadio : ''}})"
[checked]="partnerLogo.chkRadio != show_all ? checked : '' "> Clear All
</label>
</div>
我只需要在灯箱打开时将 partnerLogo.chkRadio 设置为其他内容。否则,状态将与所选电台不同步。