在浏览器刷新后如何在不使用cookie的情况下保留单选按钮和复选框值?

时间:2022-08-24 07:33:22

I have three radio buttons and 4 check boxes.

我有三个单选按钮和4个复选框。

I want to preserve the radio button and check box values after the browser refresh.

我希望在浏览器刷新后保留单选按钮和复选框值。

I do not want to use cookies (due to some other reason).

我不想使用cookies(由于其他原因)。

Could anyone please help me?

有人可以帮帮我吗?

6 个解决方案

#1


I don't think this is possible because HTTP is stateless, cookies or server side scripting provide 'state'.

我不认为这是可能的,因为HTTP是无状态的,cookie或服务器端脚本提供“状态”。

#2


You could use sessions instead.

您可以改用会话。

EDIT: My bad, I read PHP and not Javascript. However I did find this link after a quick Google search. Session variables without cookies in JS

编辑:我的坏,我读PHP而不是Javascript。但是,我在Google快速搜索后找到了这个链接。 JS中没有cookie的会话变量

#3


You might be able to use the hash url.

您可以使用哈希网址。

something like this (don't remember if you need to specify the name of the page as well, but I don't think so):

这样的事情(不记得你是否还需要指定页面的名称,但我不这么认为):

document.location.href = '#radio1=1&radio2=0' 

The hash means it just directs things on the current page and not going to another page (and the browser updates it in the address field, so if the user reloads the page, it will still be there). Then you can read it from javascript as well and set it.

哈希表示它只是指向当前页面上的内容而不是指向另一个页面(并且浏览器在地址字段中更新它,因此如果用户重新加载页面,它仍然会在那里)。然后你也可以从javascript中读取并设置它。

Not as good as using server side sessions, but it is an option :)

不如使用服务器端会话,但它是一个选项:)

#4


If you're using a form to trigger a new page loading you can make the onsubmit event call a javascript function to change the window location and append URL parameters that store the values of the radio buttons. When the page loads you would then read the parameter values from the URL. Something like this:

如果您使用表单来触发新页面加载,则可以使onsubmit事件调用javascript函数来更改窗口位置并附加存储单选按钮值的URL参数。当页面加载时,您将从URL读取参数值。像这样的东西:

<script type="text/javascript">
    function changeURL(){
        var str = window.location+"?radio1=1";
        window.open(str);
        return false;

    }
    </script>

...

<FORM onsubmit="changeURL();">
<INPUT TYPE="submit" value="click me" >
</FORM>

#5


A new facility is being developed to allow web sites to store persistent data on the client machine. Available in some browsers already this allows you to save the the radio and checkbox states and recover and restore them next time the user visits your site. For more info see here https://developer.mozilla.org/en/DOM/Storage and here http://dev.w3.org/html5/webstorage

正在开发一种新设施,以允许网站在客户端计算机上存储持久数据。在某些浏览器中已经可用,这允许您保存收音机和复选框状态,并在用户访问您的站点时恢复和恢复它们。有关详细信息,请参阅https://developer.mozilla.org/en/DOM/Storage和http://dev.w3.org/html5/webstorage

#6


Have some JS on the page submit all radiobutton/checkbox events to the server, and store their state in your database. When the page (re)loads, send this state as part of the HTML.

在页面上有一些JS将所有radiobutton / checkbox事件提交给服务器,并将它们的状态存储在数据库中。页面(重新)加载时,将此状态作为HTML的一部分发送。

#1


I don't think this is possible because HTTP is stateless, cookies or server side scripting provide 'state'.

我不认为这是可能的,因为HTTP是无状态的,cookie或服务器端脚本提供“状态”。

#2


You could use sessions instead.

您可以改用会话。

EDIT: My bad, I read PHP and not Javascript. However I did find this link after a quick Google search. Session variables without cookies in JS

编辑:我的坏,我读PHP而不是Javascript。但是,我在Google快速搜索后找到了这个链接。 JS中没有cookie的会话变量

#3


You might be able to use the hash url.

您可以使用哈希网址。

something like this (don't remember if you need to specify the name of the page as well, but I don't think so):

这样的事情(不记得你是否还需要指定页面的名称,但我不这么认为):

document.location.href = '#radio1=1&radio2=0' 

The hash means it just directs things on the current page and not going to another page (and the browser updates it in the address field, so if the user reloads the page, it will still be there). Then you can read it from javascript as well and set it.

哈希表示它只是指向当前页面上的内容而不是指向另一个页面(并且浏览器在地址字段中更新它,因此如果用户重新加载页面,它仍然会在那里)。然后你也可以从javascript中读取并设置它。

Not as good as using server side sessions, but it is an option :)

不如使用服务器端会话,但它是一个选项:)

#4


If you're using a form to trigger a new page loading you can make the onsubmit event call a javascript function to change the window location and append URL parameters that store the values of the radio buttons. When the page loads you would then read the parameter values from the URL. Something like this:

如果您使用表单来触发新页面加载,则可以使onsubmit事件调用javascript函数来更改窗口位置并附加存储单选按钮值的URL参数。当页面加载时,您将从URL读取参数值。像这样的东西:

<script type="text/javascript">
    function changeURL(){
        var str = window.location+"?radio1=1";
        window.open(str);
        return false;

    }
    </script>

...

<FORM onsubmit="changeURL();">
<INPUT TYPE="submit" value="click me" >
</FORM>

#5


A new facility is being developed to allow web sites to store persistent data on the client machine. Available in some browsers already this allows you to save the the radio and checkbox states and recover and restore them next time the user visits your site. For more info see here https://developer.mozilla.org/en/DOM/Storage and here http://dev.w3.org/html5/webstorage

正在开发一种新设施,以允许网站在客户端计算机上存储持久数据。在某些浏览器中已经可用,这允许您保存收音机和复选框状态,并在用户访问您的站点时恢复和恢复它们。有关详细信息,请参阅https://developer.mozilla.org/en/DOM/Storage和http://dev.w3.org/html5/webstorage

#6


Have some JS on the page submit all radiobutton/checkbox events to the server, and store their state in your database. When the page (re)loads, send this state as part of the HTML.

在页面上有一些JS将所有radiobutton / checkbox事件提交给服务器,并将它们的状态存储在数据库中。页面(重新)加载时,将此状态作为HTML的一部分发送。