单选框通过type="radio"实现,同组name相同以确保互斥选择;复选框使用type="checkbox",可多选,name常用数组格式;均支持value和checked属性,并可通过label提升用户体验。

在HTML中,单选框和复选框都是通过 input 标签实现的,使用不同的 type 属性值来区分。它们用于表单中让用户从多个选项中进行选择。
单选框用于一组互斥选项,用户只能选择其中一个。要创建单选框,将 input 的 type 设置为 radio。
关键点:
示例代码:
立即学习“前端免费学习笔记(深入)”;
<form>
<label>
<input type="radio" name="gender" value="male" checked> 男
</label><br>
<label>
<input type="radio" name="gender" value="female"> 女
</label><br>
<label>
<input type="radio" name="gender" value="other"> 其他
</label>
</form>
复选框允许用户选择一个或多个选项。将 input 的 type 设置为 checkbox 即可创建复选框。
特点:
示例代码:
立即学习“前端免费学习笔记(深入)”;
<form>
<label>
<input type="checkbox" name="hobby[]" value="reading" checked> 阅读
</label><br>
<label>
<input type="checkbox" name="hobby[]" value="sports"> 运动
</label><br>
<label>
<input type="checkbox" name="hobby[]" value="music"> 音乐
</label>
</form>
将文本包裹在 label 标签中,并与 input 关联,可以让用户点击文字也能选中对应项,提升操作便利性。
方式一:嵌套写法(推荐)
<label> <input type="radio" name="choice" value="1"> 选项一 </label>
方式二:使用 for 和 id 关联
<input type="checkbox" id="agree" name="agree" value="yes"> <label for="agree">我同意条款</label>
以上就是HTML单选复选框如何制作_HTML单选复选框INPUT标签的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号