简体   繁体   中英

how to manage a counter on different blades in laravel?

Hello i am working on a project i need to send item in session and add limit only 4 item will be add on session. it's working on one blade but when i move to another blade it will show nothing on the button like how many items in the session and when am trying to add another item it will add.

this is my script

var increment = 0;
$(document).ready(function(){
    $(".compare").click(function(){
        increment++;
        document.getElementById('compare').innerHTML = "";
        document.getElementById('compare').innerHTML = "Compare (" +increment+")";
                if(increment == 4)
                {
                    var array =  document.getElementsByClassName('compare');
                    for (var i = 0 ; i < array.length ; i++)
                    {
                        array[i].setAttribute('disabled','');
                    }
                }

i am wanted to add only 4 items in the session from every where. please send any solution .

Try this :

var increment = 1;
$(document).ready(function(){
  if(localStorage.getItem("compare") > 0){
    increment = localStorage.getItem("compare");
    document.getElementById('compare').innerHTML = "Compare (" +increment+")";
  } else {
    document.getElementById('compare').innerHTML = "";
  }
  $(".compare").click(function(){
    if(increment == 4)
    {
      var array =  document.getElementsByClassName('compare');
      for (var i = 0 ; i < array.length ; i++)
      {
          array[i].setAttribute('disabled','');
      }
    } else {
      increment++;
      localStorage.setItem("compare", increment);
      document.getElementById('compare').innerHTML = "Compare (" +increment+")";
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM