表单

342 阅读1分钟

表单元素:

form:定义一个表单区域,可以获取用户输入的数据

input:输入框,不同的类型会让该元素有不同的含义

select:下拉列表

option:下拉列表的选项

textarea:多行文本框

input元素中的常用属性:

input>标签规定了用户可以在其中输入数据的输入字段。元素在

元素中使用,用来声明允许用户输入数据的input控件。输入类型是由类型属性(type)定义的。

文本框(type="text")
<input type="text" placeholder="输入提示"><!--placeholder 属性表示提示用户输入的内容,输入内容后消失-->
密码框(type="password")
<input type="password" name="" id="" maxlength="6"><!--maxlength 设置可输入的长度-->
按钮(type="button")
<input type="button" value="单纯的按钮"><!--单纯的按钮,没有提交功能-->
提交按钮(type="submit")、()
<input type="submit" value="提交按钮1">
<button>提交按钮2</button><!--以上两个都具有提交功能,会发生跳转-->
重置按钮(type="reset")
<input type="reset" value="重置"><!--输入内容后点击可以清空内容,重置-->
单选框(type="radio")
<input type="radio" name="male" id=""><input type="radio" name="male" id=""><!--通过 name 属性实现单选效果,当他们具有同一个name的时候只能选一个,两个不同name的时候可以同时选-->

例2:优化效果

<label for="male"></label>
<input type="radio" name="gender" id="male">
<label for="female"></label>
<input type="radio" name="gender" id="female"><!--通过 id 绑定 label 标签的文字,使点击文字时也可以选中-->
多选框(type="checkbox")
蟹黄豆花
<input type="checkbox">
鸡公煲
<input type="checkbox">
回锅肉
<input type="checkbox">
小炒肉
<input type="checkbox">
米饭
<input type="checkbox" checked="checked">
<input type="text" disabled><!--当属性与属性值相同时可以简写如:<input type="checkbox" checked>,这个叫做标志性属性或(特殊属性)-->
<!--checked 属性表示默认选中>
<!--disabled 属性禁用>
提交文件 (type="file")
 <input type="file" name="" id="">

下拉列表:select

<select name="" id="">
    <option value="" checked>四川</option>
    <option value="">上海</option>
    <option value="">北京</option>
    <option value="">深圳</option>
    <option value="">广西</option>
    <option value="">云南</option>
    <option value="">湖南</option>
</select><!--option 表示选项-->
<!--也可以添加 checked 属性默认选中-->

多行文本框:textarea

<!-- 默认情况有下拉框,可以控制大小 -->
<textarea name="" id="" cols="30" rows="10"></textarea>
<!-- 取消默认下拉框,添加一个样式 -->
<textarea name="" id="" cols="30" rows="10" style="resize: none;"></textarea><!--添加 style="resize: none; 属性,控制多行文本框尺寸固定-->

新增表单控件

邮箱框 email
<input type="email" name="" id=""><!--如果格式不对,会报错-->
年月框 month
<input type="month" name="" id=""><!--日历可以选中年月-->
滑块 range
<input type="range" name="" id="">
只读
<input type="text" value="只读" readonly>
初始默认 selected
<select name="" id="">
    <option value="">中国</option>
    <option value="" selected>其他</option>
</select>
必填 required
<input type="password" required>