HTML表单是网页中最为基本的交互形式,它可以让用户输入信息、选择选项和提交数据到服务器。下面是一个简单的HTML表单代码示例:
<form action="submit.PHP" method="post"> <label for="name">姓名:</label> <input type="text" id="name" name="name" required><br> <label for="gender">性别:</label> <input type="radio" id="male" name="gender" value="male"> <label for="male">男</label> <input type="radio" id="female" name="gender" value="female"> <label for="female">女</label><br> <label for="age">年龄:</label> <input type="number" id="age" name="age" min="1"><br> <label for="interest">兴趣爱好:</label> <select id="interest" name="interest[]" multiple> <option value="reading">读书</option> <option value="music">音乐</option> <option value="sports">运动</option> </select><br> <label for="message">留言:</label> <textarea id="message" name="message"></textarea><br> <button type="submit">提交</button> </form>
上述代码中,form标签表示一个表单,其中action属性定义了表单提交的地址,method属性定义了提交方式,这里是POST方式。
在表单中,每个输入项都有一个对应的label标签,它的for属性与输入项的id属性相同,用于关联两者。这样做可以提高可访问性,盲人用户可以通过屏幕阅读器正确理解每个输入项的用途。
input标签是最常用的输入元素,type属性指定了输入类型,例如text、radio、number等。在这个表单中,姓名和年龄都是文本输入框,性别是单选框,兴趣爱好是下拉选择框,留言是文本域。
在最后,表单提供了一个提交按钮,用户点击它后表单数据将提交到指定的地址。如果某个输入项是必填的,可以使用required属性标记它,浏览器会在提交前验证该项是否有值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。