单击按钮如何向Cookie值添加+1

I really need your help and I know It is easy to you. I made the following code to show a modal bootstrap by loading every page of my website. but I need to show that modal just for 3 times to every user. so I need to count times the user click on the button to close modal. If the user 3 times has closed the modal, I do not want to show him the modal (popup) again. I want to do that by cookie but my code does not work and I use cookie plugin to use JQuery Bootstarp Modal.

谢谢大家的帮助。

我的HTML代码

<!-- Modal -->
        <div id="hereiakarneta" class="modal fade" role="dialog">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" id="AcceptMyGiftClose" data-dismiss="modal">&times;</button>
                <h4 class="modal-title" style="text-align: center;" >From Me To You</h4>
              </div>
              <div class="modal-body">

                <div class="hikmywords">
                    <img src="" class="img-responsive" alt="">
                    <ul>
                        <li><h5>my gift to you</h5></li>
                        <li>
                            <p>click on the button and download my gift</p>
                        </li>
                    </ul>
                </div>

              </div>
              <div class="modal-footer">
                <p><a>My Gift</a></p>
              </div>
            </div>
          </div>
        </div>  

我的JQuery代码

jQuery(document).ready(function($) {
    var VisitPopupGift = $.cookie('AcceptMyGift');
    if ( !$.cookie('AcceptMyGift') ) {

        $('#hereiakarneta').modal({
            show: true,
            backdrop: 'static',
            keyboard: false, 
        })
        $("#AcceptMyGiftClose").click(function() {
            VisitPopupGift = 1;
            $.cookie('AcceptMyGiftOnce', VisitPopupGift, {
                expires: 30,
                path: '/',
            });
            $(".hereiakarneta").fadeOut('fast');
        });

    } else { 
        if ( VisitPopupGift < 5 ) {
        $('#hereiakarneta').modal({
            show: true,
            backdrop: 'static',
            keyboard: false, 
        })
        $("#AcceptMyGiftClose").on("click",function(){
            //VisitPopupGift++;
            VisitPopupGift++;
            $.cookie('AcceptMyGiftOnce', VisitPopupGift, {
                expires: 30,
                path: '/',
            });
            $(".hereiakarneta").fadeOut('fast');
        });
        }
    }
});
评论
msed
msed

由于我不熟悉JQuery,因此想到的是,设置一个计数器来计算每次点击,然后设置一个if else语句,在if语句中,例如,如果counter小于或等于3,则显示模态,否则将其关闭或隐藏。

点赞
评论