记住单选按钮选择
我有以下代码,可以完美运行。它允许用户使用单选按钮“喜欢”或“不喜欢”每个帖子。复选框是允许用户显示/隐藏所有喜欢或不喜欢的帖子的开关。
问题是,我需要页面在用户离开和返回时记住单选按钮选择。这需要cookie吗?如果是这样,我该如何实施?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unhide on checkboxes/radio buttons</title>
<link rel="stylesheet" type="text/css" href="_styles.css" media="screen" />
<script type="text/javascript">
function radioGroupValue(groupObj)
{
//Check if there is only one option (i.e. not an array)
if (!groupObj.length)
{
return (groupObj.checked) ? groupObj.value : false;
}
//Multiple options, iterate through each option
for (var i=0; i<groupObj.length; i++)
{
if (groupObj[i].checked)
{
return groupObj[i].value;
}
}
//No option was selected
return false;
}
function toggleLayer(formObj)
{
var showLikes = document.getElementById('show_likes').checked;
var showDislikes = document.getElementById('show_dislikes').checked;
var postIndex = 1;
while(document.getElementById('post_'+postIndex))
{
var liked = radioGroupValue(formObj.elements['like_'+postIndex])
var display = ((!showLikes && liked==='1') || (!showDislikes && liked==='0')) ? 'none' : '';
document.getElementById('post_'+postIndex).style.display = display;
postIndex++;
}
}
</script>
</head>
<body>
<form action="" method="post">
<fieldset>
<legend>Unhide Layer Form</legend>
<ul>
<p><input id="show_likes" name="show_likes" type="checkbox" value="1" checked="checked" onclick="toggleLayer(this.form);" /><label for="b1">Show Likes:</label> </p>
<p><input id="show_dislikes" name="show_dislikes" type="checkbox" value="1" checked="checked" onclick="toggleLayer(this.form);" /><label for="b1">Show Disikes:</label> </p>
</ul>
<label>Email:</label>
<input type="email" />
</fieldset>
<br><br>
<fieldset>
<legend>Posts</legend>
<div id="post_1" class="post">
<b>Post #1</b><br>
Content of post #1<br>
<p><input type="radio" name="like_1" value="1"><label for="like1a">Like</label></p> <p><input type="radio" name="like_1" value="0" onclick="toggleLayer(this.form);"><label for="like1b"> Dislike</label></p>
</div>
<div id="post_2" class="post">
<b>Post #2</b><br>
Content of post #2<br>
<p><input type="radio" name="like_2" value="1"><label for="like2a">Like</label></p> <p><input type="radio" name="like_2" value="0" onclick="toggleLayer(this.form);"><label for="like2b"> Dislike</label></p>
</div>
<div id="post_3" class="post">
<b>Post #3</b><br>
Content of post #3<br>
<p><input type="radio" name="like_3" value="1"><label for="like3a">Like</label></p> <p><input type="radio" name="like_3" value="0" onclick="toggleLayer(this.form);"><label for="like3b"> Dislike</label></p>
</div>
<div id="post_4" class="post">
<b>Post #4</b><br>
Content of post #4<br>
<p><input type="radio" name="like_4" value="1"><label for="like4a">Like</label></p> <p><input type="radio" name="like_4" value="0" onclick="toggleLayer(this.form);"><label for="like4b"> Dislike</label></p>
</div>
<div id="post_5" class="post">
<b>Post #5</b><br>
Content of post #5<br>
<p><input type="radio" name="like_5" value="1"><label for="like5a">Like</label></p> <p><input type="radio" name="like_5" value="0" onclick="toggleLayer(this.form);"><label for="like5b"> Dislike</label></p>
</div>
<div id="post_6" class="post">
<b>Post #6</b><br>
Content of post #6<br>
<p><input type="radio" name="like_6" value="1"><label for="like6a">Like</label></p> <p><input type="radio" name="like_6" value="0" onclick="toggleLayer(this.form);"><label for="like6b"> Dislike</label></p>
</div>
<div id="post_7" class="post">
<b>Post #7</b><br>
Content of post #7<br>
<p><input type="radio" name="like_7" value="1"><label for="like7a">Like</label></p> <p><input type="radio" name="like_7" value="0" onclick="toggleLayer(this.form);"><label for="like7b"> Dislike</label></p>
</div>
</form>
有什么指点吗?
I have the following code that works perfectly. It allows the user to 'like' or 'dislike' each post with radio buttons. The checkboxes are switches that allow the user to show/hide all liked or disliked posts.
The problem is, I need the page to remember the radio button selections when the user leaves and returns. Would this require cookies? If so, how do i implement it?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unhide on checkboxes/radio buttons</title>
<link rel="stylesheet" type="text/css" href="_styles.css" media="screen" />
<script type="text/javascript">
function radioGroupValue(groupObj)
{
//Check if there is only one option (i.e. not an array)
if (!groupObj.length)
{
return (groupObj.checked) ? groupObj.value : false;
}
//Multiple options, iterate through each option
for (var i=0; i<groupObj.length; i++)
{
if (groupObj[i].checked)
{
return groupObj[i].value;
}
}
//No option was selected
return false;
}
function toggleLayer(formObj)
{
var showLikes = document.getElementById('show_likes').checked;
var showDislikes = document.getElementById('show_dislikes').checked;
var postIndex = 1;
while(document.getElementById('post_'+postIndex))
{
var liked = radioGroupValue(formObj.elements['like_'+postIndex])
var display = ((!showLikes && liked==='1') || (!showDislikes && liked==='0')) ? 'none' : '';
document.getElementById('post_'+postIndex).style.display = display;
postIndex++;
}
}
</script>
</head>
<body>
<form action="" method="post">
<fieldset>
<legend>Unhide Layer Form</legend>
<ul>
<p><input id="show_likes" name="show_likes" type="checkbox" value="1" checked="checked" onclick="toggleLayer(this.form);" /><label for="b1">Show Likes:</label> </p>
<p><input id="show_dislikes" name="show_dislikes" type="checkbox" value="1" checked="checked" onclick="toggleLayer(this.form);" /><label for="b1">Show Disikes:</label> </p>
</ul>
<label>Email:</label>
<input type="email" />
</fieldset>
<br><br>
<fieldset>
<legend>Posts</legend>
<div id="post_1" class="post">
<b>Post #1</b><br>
Content of post #1<br>
<p><input type="radio" name="like_1" value="1"><label for="like1a">Like</label></p> <p><input type="radio" name="like_1" value="0" onclick="toggleLayer(this.form);"><label for="like1b"> Dislike</label></p>
</div>
<div id="post_2" class="post">
<b>Post #2</b><br>
Content of post #2<br>
<p><input type="radio" name="like_2" value="1"><label for="like2a">Like</label></p> <p><input type="radio" name="like_2" value="0" onclick="toggleLayer(this.form);"><label for="like2b"> Dislike</label></p>
</div>
<div id="post_3" class="post">
<b>Post #3</b><br>
Content of post #3<br>
<p><input type="radio" name="like_3" value="1"><label for="like3a">Like</label></p> <p><input type="radio" name="like_3" value="0" onclick="toggleLayer(this.form);"><label for="like3b"> Dislike</label></p>
</div>
<div id="post_4" class="post">
<b>Post #4</b><br>
Content of post #4<br>
<p><input type="radio" name="like_4" value="1"><label for="like4a">Like</label></p> <p><input type="radio" name="like_4" value="0" onclick="toggleLayer(this.form);"><label for="like4b"> Dislike</label></p>
</div>
<div id="post_5" class="post">
<b>Post #5</b><br>
Content of post #5<br>
<p><input type="radio" name="like_5" value="1"><label for="like5a">Like</label></p> <p><input type="radio" name="like_5" value="0" onclick="toggleLayer(this.form);"><label for="like5b"> Dislike</label></p>
</div>
<div id="post_6" class="post">
<b>Post #6</b><br>
Content of post #6<br>
<p><input type="radio" name="like_6" value="1"><label for="like6a">Like</label></p> <p><input type="radio" name="like_6" value="0" onclick="toggleLayer(this.form);"><label for="like6b"> Dislike</label></p>
</div>
<div id="post_7" class="post">
<b>Post #7</b><br>
Content of post #7<br>
<p><input type="radio" name="like_7" value="1"><label for="like7a">Like</label></p> <p><input type="radio" name="like_7" value="0" onclick="toggleLayer(this.form);"><label for="like7b"> Dislike</label></p>
</div>
</form>
Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
localstorage
:所有现代浏览器都支持
localstorage
,包括IE8。对于旧版本的 IE,您可以轻松添加支持(在上述脚本之前)。
这是小提琴。使用单选按钮,关闭窗口,然后重新打开它(或只是刷新)。您会看到他们保留了自己的状态。
You can use
localstorage
:localstorage
is supported in all modern browsers, including IE8.For older versions of IE, you can add support easily (before the above script).
Here's the fiddle. Play around with the radio buttons, close the window, then re-open it (or just refresh). You'll see that they retain their state.
如果您希望其他人看到点赞数(如计数),请考虑使用 AJAX 发送数据到某处的存储数据库。这是可以随页面一起加载的。如果您只对存储单个用户的选择感兴趣,我仍然会推荐 AJAX,但不要保存计数器,而是保存每个用户的是/否答案并将其发布到数据库中的表中。为了节省空间,最好只存储“是”答案并假设其他答案为“否”。
If you want others to see the likes (as in a count) look into using AJAX to send the data to a storage database somewhere. This was it can be loaded with the page. If you are only interested in storing the individual users choices I would still recommend AJAX but instead of saving a counter save the yes/no answer for each user and post into a table in a database. To save space it would be better perhaps to only store the yes answers and assume the others to be a no.
更好的方法是将其实现为数据库解决方案,而不是 cookie。 Cookie 可以被删除,因此这将成为一个不一致的解决方案。如果您希望只要用户在系统中处于活动状态,就保留此选择,则将用户选择保存在数据库中,并围绕他们的保存喜好和喜好实现渲染逻辑。不喜欢。
Instead of cookies, a better approach would be to implement this as a database solution. Cookies can be deleted and thus this will become a inconsistent solution. If your desire it to persist this selection for as long as the user is active in your system, then save you user selection in the database and implement your rendering logic around their save likes & dislikes.