Tell me, please, how is it possible to record, via javascript, the active state of a radio button in a session so that it is stored, and when switching to new pages on the site, take that value so that it is selected earlier?

  • lampa, the repository and buttons work here, but it does not tell the storage sorting script ... yalon.su there are categories there, when updating it remembers the radio button, but does not give the sorting script a message about the activity of this radio button - ivanforpw nov
  • today from morning I will try to look. - lampa
  • come up with? - ivanforpw
  • @lampa, hmm, replaced the code, but it doesn't work, maybe I didn't understand what jquery ready is, but I replaced the line var $ filteredData = $ data.find ('li [data-type =' + $ ($ filterType + " : checked "). val () + ']'); - ivanforpw
  • @ivanforpw did not need to be replaced, and vice versa - to insert after when. jquery ready is a construct: $ (function () {...}); - lampa

2 answers 2

You can save server resources and use localstorage. When changing the checkbox, write the necessary data to the stack. When updating the page, we look into the stack and look for the desired values.

<script type="text/javascript"> function SetData (elem) { alert(elem.getAttribute('id')); localStorage.setItem(elem.getAttribute('name'), elem.getAttribute('id')); alert(localStorage.getItem(elem.getAttribute('name'))); } function GetData (item) { return localStorage.getItem(item); } /** * следующий код выполняется после загрузки страницы. */ window.onload = function() { /** * узнаем, какое значение у нас в localStorage.getItem(item) */ var Item = GetData ('type'); alert(Item); /** * если значение установлено (!undefined) */ if(Item != undefined) { document.getElementById(Item).checked = true; } } </script> <input type="radio" name="type" value="all" checked id='AllCheck' onchange="SetData(this);"> Все <br> <input type="radio" name="type" value="blue" id='BlueCheck' onchange="SetData(this);"> Синий <br> <input type="radio" name="type" value="red" id='RedCheck' onchange="SetData(this);"> Красный <br> 

Add this code to the very end of jquery ready.

 var $filteredData = $data.find('li[data-type='+GetData ('type')+']'); // вызываем плагин $applications.quicksand($filteredData, { duration: 800, easing: 'easeInOutQuad' }); 
  • help to make out with js, please, just have not started to teach him yet, but here you need to implement a small script. button name type <script type = "text / javascript"> function SetData {localStorage.setItem ('type', 'how can I record information here, what is stored in value?'); } </ script> <script type = "text / javascript"> function GetData {var type = localStorage.getItem ('type'); } </ script> - ivanforpw
  • <input type = \ "radio \" name = \ "type \" value = \ "all \" checked> All <br> <input type = \ "radio \" name = \ "type \" value = \ "blue \ "> Blue <br> <input type = \" radio \ "name = \" type \ "value = \" red \ "> Red <br> And then how to set its position through a function? - ivanforpw
  • updated post. - lampa
  • yalon.su/test.html seems to be something wrong - ivanforpw
  • Oh, I and dolbaeb, I found one more mistake. Kapets. The value received in the GetData function did not return. Updated post. - lampa

With php: At the beginning of the page you start the session

  session_start(); 

Then you add the value of the selected button to the session.

 $_SESSION['Ch'] = $Chbtn; 

and then using if / case, depending on the value of this variable, you set the value of the radio button

  • @lampa, added, look yalon.su here, but still does not work - ivanforpw