表单
表单类型:
email :能够验证当前输入的邮箱地址是否合法
url : 验证URL
number : 只能输入数字,其他输入不了,而且自带上下增大减小箭头,max属性可以设置为最大值,min可以设置为最小值,value为默认值。
search : 输入框后面会给提供一个小叉,可以删除输入的内容,更加人性化。
range : 可以提供给一个范围,其中可以设置max和min以及value,其中value属性可以设置为默认值
color : 提供了一个颜色拾取器
time : 时分秒
data : 日期选择年月日
datatime : 时间和日期(目前只有Safari支持)
datatime-local :日期时间控件
week :周控件
month:月控件
表单属性:
placeholder :提示信息
autofocus :自动获取焦点
autocomplete=“on” 或者 autocomplete=“off” 使用这个属性需要有两个前提:
表单必须提交过
必须有name属性。
required:要求输入框不能为空,必须有值才能够提交。
pattern=" " 里面写入想要的正则模式,例如手机号patte="^(+86)?\d{10}$"
multiple:可以选择多个文件或者多个邮箱
form=" form表单的ID"
表单事件:
oninput 每当input里的输入框内容发生变化都会触发此事件。
oninvalid 当验证不通过时触发此事件。
附上简单在线代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>form</title> </head> <body> <form action="/" method="get"> <input type="text" autocomplete="on" name="name" required> <input type="text" pattern="^1[3,5,6,7,8,9]\d{9}" name="code" required> <input type="email" placeholder="email input"><br /> <input type="url" placeholder="url input"><br /> <input type="number" max="100" min="10" placeholder="number input" style="width: 200px"><br> <input type="search"><br> <input type="color" id="color"> <input type="date"> <input type="submit" value="submit"> </form> <script src="https://code.jquery.com/jquery-3.6.0.js"></script> <script> $(document).ready(function (){ $('#color').change(function (event){ console.log(`当前选中颜色值:${event.target.value}`) }) }) </script> </body> </html>
标签: JavaScript
还木有评论哦,快来抢沙发吧~