如何在单击按钮时将 +1 添加到 cookie 值

How to Add +1 to a cookie value on click of a button

我真的需要你的帮助,我知道这对你来说很容易。我制作了以下代码,通过加载我网站的每个页面来显示 模态 bootstrap。但我只需要向每个用户显示该模式 3 次。所以我需要计算用户点击 按钮关闭模式 的次数。如果用户 3 次关闭了模态,我不想再次向他显示模态(弹出窗口)。我想通过 cookie 做到这一点,但我的代码不起作用,我使用 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');
        });
        }
    }
});

因为我对JQuery不熟悉,所以想到的是,设置一个计数器,计算每次点击,然后设置一个if else语句,在if语句中,如果计数器小于大于或等于 3,显示模式,否则关闭或隐藏它。

幸运的是我找到了这个并解决了问题:
我想在我的网站上使用它:https://karneta.com/ 但不使用 wordpress 插件 :)

jQuery(document).ready(function($) {
    //set cookie
    var exp = $.cookie("exp");
    //set cookie 0 for first
    exp = (exp)?parseInt(exp,10):0;

    if ( exp <= 2 ) {
        $('#hereiakarneta').modal({
            show: true,
            backdrop: 'static',
            keyboard: false, 
        })
    } else if ( exp >= 3 ) {
        $(".dologinfirst").delay(2000).fadeIn(500);
        $("#menubutton").click(function(){
            $(".dologinfirst").hide();
        });
        $('body').click(function() {
            $(".dologinfirst").hide();
        });
    }

    //getting the value of cookie and assigning it to a variable
    $("#AcceptMyGiftClose").one("click",function(){ // use .on if you want every click to count
    exp++;
    //set the incremented value of CookieCount variable to the cookie
    $.cookie("exp",exp,{ expires: 30, path: '/' }); // expiry date 30 days

  });
});