<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 单选框 -->
<div>性别:
<label><input type="radio" checked name="sex" value="男生">男生</label>
<label><input type="radio" name="sex" value="女生">女生</label>
</div>
<!-- 复选框 -->
<div>
爱好:
<label><input type="checkbox" name="like" value="0">音乐</label>
<label><input type="checkbox" name="like" value="1">旅游</label>
</div>
<!-- label和input -->
<div>
<label for="ddd">绑定
<input type="text" id="ddd">
</label>
</div>
</body>
</html>
<script>
let radio=document.querySelector("input[type=radio]").checked
let radios=document.querySelectorAll("input[type=radio]")
radios.forEach((v,i)=>{
v.onclick=function(){
console.log(v.value)
console.log(v.checked)
}
})
</script>
网友评论