表单上手第一招:先把“收快递”的三件事写明白
76
0
0
别把表单想得太神秘,它就仨核心问题:
- 填完往哪送?——
action - 怎么送过去?——
method - 信封啥格式?——
enctype
把这三行写利索,剩下的全是“摆控件”。
1. 最小能跑的骨架
<form action="register.php" method="post">
<label>用户名 <input name="username" required></label>
<label>密码 <input type="password" name="pwd" required></label>
<button>注册</button>
</form>浏览器立刻给你一张“能点提交”的页面。别的都是锦上添花。
2. 常用控件一句话记忆
<input>→ 单行小框,类型换一行键盘就变脸<textarea>→ 多行大框,写留言、评价格外香<select>→ 下拉列表,选项多但不占地儿<button>→ 比<input type="submit">好看,还能塞图标
3. 几个顺手属性
required→ 空着不让走,前端先挡一刀placeholder→ 灰字提示,别当标签用maxlength→ 字数刹车站,手机端尤其爽autocomplete="off"→ 不让浏览器瞎填,支付密码常用
4. 一段能直接搬的“注册小卡片”
<form action="save.php" method="post" enctype="multipart/form-data">
<label>用户名
<input name="username" required maxlength="15" autocomplete="username">
</label>
<label>邮箱
<input type="email" name="email" required autocomplete="email">
</label>
<label>密码
<input type="password" name="pwd" required minlength="6" autocomplete="new-password">
</label>
<label>头像
<input type="file" name="avatar" accept="image/*">
</label>
<button type="submit">创建账号</button>
</form>把 enctype 写对,后台才能收到图;把 autocomplete 写全,iPhone 自动填充才不迷路。
5. 最后喊句话
表单开发没有玄学:先写“送快递”的三要素,再摆控件、加提示、做校验。
别让技术细节跑到用户面前——他们只想点两下就完事,剩下的,全靠你把属性贴到位。
0
快来点个赞吧
发表评论
评论列表