Check / uncheck radio input field not updated properly on AMP

戏子无情 提交于 2021-01-28 06:08:54

问题


I have developed an AMP site where I have three radio buttons.

  1. show all
  2. show only favourite
  3. clear all

when i will click on clear all button it will show a lightbox. On that lightbox I have two buttons.

  1. ok
  2. Cancel

when user click on cancel from the lightbox the show all radio input will be checked. I have implemented it with amp-state variable change. For the very first time when i click on cancel it checked properly in show all radio input but after that if I have click on clear radio input and then cancel button again it does not checked again show all radio input.

Here i have added a sample code in codepen

Sample Code 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>

any suggestion please?


回答1:


Well, at last, I found the solution to this problem from amp-team. I am posting that answer so that if anyone faces the same problem they can solve it easily

<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>

I just need to set the partnerLogo.chkRadio to something else when lightbox opens. Otherwise, the state would be out of sync with selected radio.



来源:https://stackoverflow.com/questions/51056558/check-uncheck-radio-input-field-not-updated-properly-on-amp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!