【前端】input radio多选事件获取所有选中的id,radio样式优化可修改
获取选中radio的长度及各个值:
$("#all_button").on('click', function() {
obj = document.getElementsByClassName("input_radio_checked");
str = "";
for(i = 0; i < obj.length; i++) {
str += obj[i].id + " "; //这里是数字之间的空格
}
console.log(str)
alert(str)
})
radio样式优化如下:
radio样式优化css
<!--新增选择-->
<div class="radio_input"><input id="1" type="radio" class="input_radio" name="1" value="2"><label for="1"></label></div>
<!---->
css如下:
/*radio*/
.radio_input {
width: 8%;
position: relative;
line-height: 30px;
margin-top: 1.4rem;
margin-left: 0.6rem;
}
.radio_input{
position: relative;
line-height: 30px;
margin-top: 1.4rem;
margin-left: 0.6rem;
}
.radio_input{
position: relative;
line-height: 30px;
margin-top: 1.4rem;
margin-left: 0.6rem;
}
input[type="radio"] {
width: 20px;
height: 20px;
opacity: 0;
}
label {
position: absolute;
left: 5px;
top: 3px;
width: 20px;
height: 20px;
border-radius: 50%;
border: 1px solid #999;
}
/*设置选中的input的样式*/
/* + 是兄弟选择器,获取选中后的label元素*/
.get_top_color {
background-color: #fe6d32;
border: 1px solid #fe6d32;
}
.get_top_color::after {
position: absolute;
content: "";
width: 5px;
height: 10px;
top: 3px;
left: 6px;
border: 2px solid #fff;
border-top: none;
border-left: none;
transform: rotate(45deg)
}
选中事件的js:
<script type="text/javascript">
$(".input_radio").click(function() {
$(this).addClass('input_radio_checked');
$(this).parent().find('label').addClass('get_top_color');
var domName = $(this).attr('name'); //获取当前单选框控件name 属性值
var checkedState = $(this).attr('checked'); //记录当前选中状态
$("input:radio[name='" + domName + "']").attr('checked', false); //1.
$(this).attr('checked', true); //2.
if(checkedState == 'checked') {
$(this).attr('checked', false); //3.
$(this).parent().find('label').removeClass('get_top_color');
$(this).removeClass('input_radio_checked');
} else {
}
});
</script>
效果:
本文来自博客园,作者:JeckHui;
个人主页:前端实用站点推荐; 热榜资讯早读:热榜资讯-导航;
转载请注明原文链接:https://www.cnblogs.com/xiaohuizhang/p/8624011.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)