【发布时间】:2012-06-24 08:20:32
【问题描述】:
我正在使用上一篇文章中提到的本地存储想法:remember radio button selections
他们的 jfiddle 示例工作正常,但是当我将相同的代码(见下文)合并到本地主机上的网页中时,刷新页面后新选择不起作用。我正在引用最新的 jquery 库 - http://code.jquery.com/jquery-latest.js
如有任何建议,我将不胜感激。我确定这是我缺少的一些简单的东西。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function()
{
$('input[type=radio]').each(function()
{
var state = JSON.parse( localStorage.getItem('radio_' + this.id) );
if (state) this.checked = state.checked;
});
});
$(window).bind('unload', function()
{
$('input[type=radio]').each(function()
{
localStorage.setItem(
'radio_' + this.id, JSON.stringify({checked: this.checked})
);
});
});
</script>
</head>
<body>
<form method="post">
<input type="radio" name="foo" id="foo1" />foo1<br />
<input type="radio" name="foo" id="foo2" />foo2<br />
<input type="radio" name="bar" id="bar1" />bar1<br />
<input type="radio" name="bar" id="bar2" />bar2<br />
</form>
</body>
【问题讨论】:
-
您是否使用与 jfiddle 示例中完全相同的代码?如果您不发布任何代码,那真的很难提供帮助。
-
您的确切代码是什么?我们无法猜测您做错了什么,我们必须分析您的实际代码。
-
代码现在添加到初始问题中。干杯。
-
我认为如果您不添加 HTML5
doctype,HTML5 的localStorage将无法在大多数浏览器中运行。
标签: jquery radio-button local-storage