获取选中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 {
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"] {
20px;
height: 20px;
opacity: 0;
}
label {
position: absolute;
left: 5px;
top: 3px;
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: "";
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>
效果: