javascript – 单击其他按钮后取消选中单选按钮
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 单击其他按钮后取消选中单选按钮,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6101字,纯文字阅读大概需要9分钟。
内容图文
![javascript – 单击其他按钮后取消选中单选按钮](/upload/InfoBanner/zyjiaocheng/733/0afbb0420b7b4aa18c292e626b64cd9b.jpg)
我正在制作一个带有两个按钮的投票系统(upvote / downvote).我已经使用输入和标签来创建它,但是有一个问题:
如果我使用单选按钮,我可以向上或向下投票,但我不能不投票.
如果我使用复选框,我可以取消投票,但我也可以上下投票.
我想要实现的是只能投票或者投票,同时还能够取消投票(想想reddit).
我已经彻底找到了答案,我发现这不可能与CSS有关.有很多脚本,问题是我不知道JavaScript或jQuery,所以我不知道如何实现代码.
我认为最简单的解决方案是在点击upvote时取消选中downvote按钮,反之亦然,但同样,我不知道这段代码的样子或如何实现它.
也许有一个更容易的解决方案使用纯CSS来改变其他按钮的外观,即使它被检查后?我不知道,但如果它存在,我宁愿使用这样的解决方案.
我的代码:
input[type=radio] {
display: none
}
#up {
width: 20px;
height: 16px;
background: url(http://c.thumbs.redditmedia.com/Y5Gt7Gtk59BlV-3t.png) left top;
position: absolute;
left: 50px;
top: 50px
}
#down {
width: 20px;
height: 16px;
background: url(http://c.thumbs.redditmedia.com/Y5Gt7Gtk59BlV-3t.png) left bottom;
opacity: 0.5;
position: absolute;
left: 50px;
top: 84px
}
#vote {
width: 21px;
height: 18px;
text-align: center;
font-size: 26px;
line-height: 18px;
position: absolute;
left: 50px;
top: 66px;
z-index: -1
}
#up:hover {
background-position: top;
cursor: pointer
}
#upvote:checked ~ #up {
background-position: top;
}
#upvote:checked ~ #vote {
color: #DC5B28;
z-index: 1
}
#down:hover {
background-position: bottom;
cursor: pointer;
opacity: 1
}
#downvote:checked ~ #down {
background-position: bottom;
opacity: 1
}
#downvote:checked ~ #vote {
color: #3580DD
}
#no {
position: absolute;
display: none;
background: url(http://c.thumbs.redditmedia.com/Y5Gt7Gtk59BlV-3t.png)
}
#upvote:checked ~ #no {
display: block;
background-position: left top;
left: 50px;
top: 50px
}
#downvote:checked ~ #no {
display: block;
background-position: left bottom;
left: 50px;
top: 84px;
opacity: 0.5
}
<input type="radio" name="vote" value="+1" id="upvote">
<label for="upvote" id="up"></label>
<input type="radio" name="vote" value="-1" id="downvote">
<label for="downvote" id="down"></label>
<input type="radio" name="vote" value="0" id="novote">
<label for="novote" id="no"></label>
<div id="vote">?</div>
解决方法:
如果您想要一个仅限CSS的解决方案,您可以尝试添加一个默认隐藏的第三个选项,它代表没有投票.
然后,当用户upvotes或downvotes时,显示第三个选项,并且它与所选选项重叠.
因此,当用户认为他再次点击所选择的选项时,他实际上是选择无投票选项.
#vote {
position: relative;
}
#vote > input {
display: none; /* Hide radio buttons */
}
#vote > label {
cursor: pointer;
display: block;
width: 0;
border: 50px solid; /* We will make them look like... */
border-color: black transparent; /* ...triangles using borders */
}
#upvote + label {
border-top: none; /* Triangulating */
}
#downvote + label {
border-bottom: none; /* Triangulating */
margin-top: 15px; /* Space between triangles */
}
#vote > input:checked + label {
border-color: orange transparent; /* Highlight chosen option */
}
#vote > #novote-label {
display: none; /* Hide it by default */
position: absolute; /* Take it out of the normal flow */
border-top: none;
border-color: transparent;
}
#upvote:checked ~ #novote-label { /* Display it overlapping upvote */
display: block;
top: 0;
}
#downvote:checked ~ #novote-label { /* Display it overlapping downvote */
display: block;
bottom: 0;
}
<div id="vote">
<input type="radio" name="vote" value="+1" id="upvote" />
<label for="upvote"></label>
<input type="radio" name="vote" value="-1" id="downvote" />
<label for="downvote"></label>
<input type="radio" name="vote" value="0" id="novote" />
<label for="novote" id="novote-label"></label>
</div>
几乎没有变化,它也可以通过键盘访问:
#vote {
position: relative;
}
#vote > input { /* Hiding in a keyboard-accesible way */
opacity: 0;
position: absolute;
z-index: -1;
}
#vote > input:focus + label {
outline: 1px dotted #999; /* Keyboard friendly */
}
#vote > label {
cursor: pointer;
display: block;
width: 0;
border: 50px solid; /* We will make them look like... */
border-color: black transparent; /* ...triangles using borders */
}
#upvote + label {
border-top: none; /* Triangulating */
}
#downvote + label {
border-bottom: none; /* Triangulating */
margin-top: 15px; /* Space between triangles */
}
#vote > input:checked + label {
border-color: orange transparent; /* Highlight chosen option */
}
#vote > #novote-label {
display: none; /* Hide it by default */
position: absolute; /* Take it out of the normal flow */
border-top: none;
border-color: transparent;
}
#upvote:checked ~ #novote-label { /* Display it overlapping upvote */
display: block;
top: 0;
}
#downvote:checked ~ #novote-label { /* Display it overlapping downvote */
display: block;
bottom: 0;
}
<div id="vote">
<input type="radio" name="vote" value="+1" id="upvote" />
<label for="upvote"></label>
<input type="radio" name="vote" value="-1" id="downvote" />
<label for="downvote"></label>
<input type="radio" name="vote" value="0" id="novote" />
<label for="novote" id="novote-label"></label>
</div>
上面的片段使用边框来模拟三角形/箭头.类似地,也可以使用背景图像.
#vote {
position: relative;
}
#vote > input {
display: none;
}
#vote > label {
cursor: pointer;
display: block;
width: 20px;
height: 16px;
background-image: url('http://i.stack.imgur.com/36F2b.png');
}
#vote > #up {
background-position: left top;
}
#vote > #down {
background-position: left bottom;
}
#upvote:checked ~ #up {
background-position: top;
}
#downvote:checked ~ #down {
background-position: bottom;
}
#vote > #no {
display: none;
position: absolute;
background: none;
}
#upvote:checked ~ #no {
display: block;
top: 0;
}
#downvote:checked ~ #no {
display: block;
bottom: 0;
}
<div id="vote">
<input type="radio" name="vote" value="+1" id="upvote" />
<label for="upvote" id="up"></label>
<input type="radio" name="vote" value="-1" id="downvote" />
<label for="downvote" id="down"></label>
<input type="radio" name="vote" value="0" id="novote" />
<label for="novote" id="no"></label>
</div>
内容总结
以上是互联网集市为您收集整理的javascript – 单击其他按钮后取消选中单选按钮全部内容,希望文章能够帮你解决javascript – 单击其他按钮后取消选中单选按钮所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。