单击链接时使用 Jquery 更新 PHP 会话变量

发布于 2024-12-09 11:21:29 字数 241 浏览 0 评论 0原文

我有几个 div,用户可以使用 jquery 切换方法最小化或展开它们。然而,当页面刷新时,Div 会返回到默认状态。他们是让浏览器记住 div 最后状态的一种方法吗?

例如,如果我展开 ID 为“my_div”的 div,然后单击页面上的其他内容,然后返回原始页面,我希望“my_div”保持展开状态。

我在想可以为此使用会话变量,也许当用户单击展开/最小化按钮时,可以发送 AJAX 请求并切换会话变量...IDK..有什么想法吗?

I have several divs that a user can Minimize or Expand using the jquery toggle mothod. However, when the page is refreshed the Divs go back to their default state. Is their a way to have browser remember the last state of the div?

For example, if I expand a div with an ID of "my_div", then click on something else on the page, then come back to the original page, I want "my_div" to remain expanded.

I was thinking it would be possible to use session variables for this, perhaps when the user clicks on the expand/minimize button a AJAX request can be sent and toggle a session variable...IDK..any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无法言说的痛 2024-12-16 11:21:29

不需要 ajax 请求,只需将信息存储在 cookie 或本地存储中。
这是一个可以帮助您的库: http://www.jstorage.info/

一些示例代码(未经测试):

// stores the toggled position
$('#my_div').click(function() {
    $('#my_div').toggle();
    $.jStorage.set('my_div', $('#my_div:visible').length);
});

// on page load restores all elements to old position
$(function() {
    var elems = $.jStorage.index();
    for (var i = 0, l = elems.length; i < l; i++) {
        $.jStorage.get(i) ? $('#' + i).show() : hide();
    }
});

There's no need for an ajax request, just store the information in a cookie or in the localstorage.
Here's a library which should help you out: http://www.jstorage.info/

Some sample code (untested):

// stores the toggled position
$('#my_div').click(function() {
    $('#my_div').toggle();
    $.jStorage.set('my_div', $('#my_div:visible').length);
});

// on page load restores all elements to old position
$(function() {
    var elems = $.jStorage.index();
    for (var i = 0, l = elems.length; i < l; i++) {
        $.jStorage.get(i) ? $('#' + i).show() : hide();
    }
});
糖果控 2024-12-16 11:21:29

如果您不需要支持旧版浏览器,可以使用 html5 Web 存储

你可以这样做(示例取自 w3schools):

以下示例计算用户访问某个网站的次数
页面,在当前会话中:

<script type="text/javascript">
if (sessionStorage.pagecount) {
  sessionStorage.pagecount=Number(sessionStorage.pagecount) +1;
}
else {
  sessionStorage.pagecount=1;
}
document.write("Visits "+sessionStorage.pagecount+" time(s) this session.");
</script>

If you don't need to support old browsers, you can use html5 web storage.

You can do things like this (example taken from w3schools):

The following example counts the number of times a user has visited a
page, in the current session:

<script type="text/javascript">
if (sessionStorage.pagecount) {
  sessionStorage.pagecount=Number(sessionStorage.pagecount) +1;
}
else {
  sessionStorage.pagecount=1;
}
document.write("Visits "+sessionStorage.pagecount+" time(s) this session.");
</script>
来日方长 2024-12-16 11:21:29

其他人已经给出了与 cookie 和本地存储 API 相关的有效答案,但根据您对问题的评论,以下是如何将 click 事件处理程序附加到链接:

$("#someLinkId").click(function() {
    $.post("somewhere.php", function() {
        //Done!
    });
});

事件处理程序函数将运行每当单击它所附加的元素时。在事件处理程序中,您可以运行任何您喜欢的代码。在此示例中,将向 somewhere.php 触发 POST 请求。

Others have already given valid answers related to cookies and the local storage API, but based on your comment on the question, here's how you would attach a click event handler to a link:

$("#someLinkId").click(function() {
    $.post("somewhere.php", function() {
        //Done!
    });
});

The event handler function will run whenever the element it is attached to is clicked. Inside the event handler, you can run whatever code you like. In this example, a POST request is fired to somewhere.php.

穿越时光隧道 2024-12-16 11:21:29

我有类似的东西,我根据用户登录使用cookie
如果您只想要主 div,请不要使用

      $('#'+div_id).next().css('display','none');  

*

      $('#'+div_id).css('display','none'); 

这里是代码 *

 //this is the div 
 <div id = "<?php echo $user; ?>1" onclick="setCookie(this.id)" ><div>My Content this will hide/show</div></div>


function setCookie(div_id)
   {
var value = ''; 
var x = document.getElementById(div_id); 
var x = $('#'+div_id).next().css('display'); 
if(x == 'none')
{
    value = 'block'; 
}
else
{
    value = 'none';
}
console.log(div_id+"="+value+"; expires=15/02/2012 00:00:00;path=/")
//alert(x); 
document.cookie = div_id+"="+value+"; expires=15/02/2012 00:00:00;path=/";

   }


    function getCookie(div_id)
    {
console.log( div_id ); 
  var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
 {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
if (x==div_id)
{
   return unescape(y);
   }
  }
}

 function set_status()
 {

var div_id = ''; 

for(var i = 1; i <= 9 ; i++)
{
    div_id = '<?php echo $user; ?>'+i; 


    if(getCookie(div_id) == 'none')
    {

        $('#'+div_id).next().css('display','none'); 
    }
    else if(getCookie(div_id) == 'block') 
    {
        $('#'+div_id).next().slideDown(); 
    }

}

}

$(document).ready(function(){
    get_status();
 });

I had something like this and I used cookies based on which user logged in
if you want only the main div don't use the

      $('#'+div_id).next().css('display','none');  

use

      $('#'+div_id).css('display','none'); 

*Here is the code *

 //this is the div 
 <div id = "<?php echo $user; ?>1" onclick="setCookie(this.id)" ><div>My Content this will hide/show</div></div>


function setCookie(div_id)
   {
var value = ''; 
var x = document.getElementById(div_id); 
var x = $('#'+div_id).next().css('display'); 
if(x == 'none')
{
    value = 'block'; 
}
else
{
    value = 'none';
}
console.log(div_id+"="+value+"; expires=15/02/2012 00:00:00;path=/")
//alert(x); 
document.cookie = div_id+"="+value+"; expires=15/02/2012 00:00:00;path=/";

   }


    function getCookie(div_id)
    {
console.log( div_id ); 
  var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
 {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
if (x==div_id)
{
   return unescape(y);
   }
  }
}

 function set_status()
 {

var div_id = ''; 

for(var i = 1; i <= 9 ; i++)
{
    div_id = '<?php echo $user; ?>'+i; 


    if(getCookie(div_id) == 'none')
    {

        $('#'+div_id).next().css('display','none'); 
    }
    else if(getCookie(div_id) == 'block') 
    {
        $('#'+div_id).next().slideDown(); 
    }

}

}

$(document).ready(function(){
    get_status();
 });
凉风有信 2024-12-16 11:21:29

看看 JavaScript Cookie 方法,您可以保存 div 的当前状态,并在用户返回站点时恢复它。
有一个很好的 jQuery 插件用于处理 Cookies (http://plugins.jquery.com/project/Cookie)

希望它有帮助

Look about the JavaScript Cookie Method, you can save the current states of the divs, and restore it if the User comes back on the Site.
There is a nice jQuery Plugin for handling Cookies (http://plugins.jquery.com/project/Cookie)

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文