前端页面交互经常用到需要遍历input=[checkbox]或者input=[radio]选中值的需求,用原生js写太麻烦,最近一个项目学院这个功能,就顺便总结一下单选和多选框选中值的遍历;
//radio使用
$('input[type="radio"]').each(function(){
$(this).click(function(){
if($(this).is(":checked")){
var Val =($(this).val());
alert(Val);
}
})
});
//checkbox & radio 通用
$("input[name='wq2']").change(function(){
var res = "";
$("input[name='wq2']").each(function(){
if($(this).is(":checked")){
res += $(this).attr("value");
}
});
$("#inp2").val(res);
console.log(res);
});