<input>的type属性

本文介绍了HTML表单中各种input类型的使用方法及其应用场景,包括文本输入、密码字段、单选按钮、复选框、重置按钮等,并通过示例展示了如何在网页中正确使用这些表单元素。

type 属性规定 input 元素的类型。

语法:

<input type="value">

 

text    定义用户可输入文本的单行输入字段。

<form action="/example/html/form_action.asp" method="get">
  <p>Email: <input type="text" name="email" /></p>
  <p>Pin: <input type="text" name="pin" maxlength="18" /></p>
  <input type="submit" value="Submit" />
</form>


 

Password  定义密码字段。密码字段中的字符会被掩码(显示为星号或原点)。

<form action="/example/html/form_action.asp" method="get">
  Email: <input type="text" name="email" /><br />
  Password: <input type="password" name="pwd" maxlength="8" /><br />
  <input type="submit" value="Submit" />
</form>


 

Radio  定义单选按钮。单选按钮允许用户选取给定数目的选择中的一个选项。

<form action="/example/html/form_action.asp" method="get">
  <input type="radio" /> 苹果<br />
  <input type="radio" /> 香蕉<br />
  <input type="submit" value="Submit" />
</form>


 

Checkbox    定义复选框。复选框允许用户在一定数目的选择中选取一个或多个选项。

<form action="/example/html/form_action.asp" method="get">
<input type="checkbox" name="fruits"  /> 苹果<br />
<input type="checkbox" name="fruits" /> 香蕉<br />
<input type="checkbox" name="fruits"  /> 梨<br />
<input type="submit" value="Submit" />
</form>

 

 

Reset   定义重置按钮。重置按钮会清除表单中的所有数据。

<input type="reset" />

  

Button   定义可点击的按钮,但没有任何行为。

<input type="button" />

 

Submit  定义提交按钮。提交按钮用于向服务器发送表单数据。数据会发送到表单的 action 属性中指定的页面。

<form action="/example/html/form_action.asp" method="get">
  <p>Email: <input type="text" name="email" /></p>
  <p>Pin: <input type="text" name="pin" maxlength="18" /></p>
  <input type="submit" value="Submit" />
</form>

 

 单击确认按钮,输入会发送到服务器上名为 "form_action.asp" 的页面。

  • 大小: 1.8 KB
  • 大小: 1.8 KB
  • 大小: 1.9 KB
  • 大小: 1.9 KB
  • 大小: 1.7 KB
为了满足您的需求,我们可以基于提供的HTML结构,并结合jQuery编写相应的脚本来完成操作。以下是具体的实现步骤以及代码示例: ### 1. **点击thead标签中的总复选框** - 当用户点击 `thead` 中的总复选框时,所有位于 `tbody` 的分复选框将被设置为相同的选中状态。 ### 2. **点击tbody标签中的任意分复选框** - 检查是否所有的分复选框都处于选中状态: - 若全是选中,则自动勾选总复选框; - 否则取消总复选框的选择。 下面是完整的 jQuery 实现代码: ```html &lt;script src="https://code.jquery.com/jquery-3.6.0.min.js">&lt;/script> &lt;table border="1"> &lt;thead> &lt;tr> &lt;th>&lt;input type="checkbox" id="selectAll"> 全选&lt;/th> &lt;th>序号&lt;/th> &lt;/tr> &lt;/thead> &lt;tbody> &lt;tr> &lt;td>&lt;input type="checkbox" class="itemCheckbox">&lt;/td> &lt;td>1&lt;/td> &lt;/tr> &lt;tr> &lt;td>&lt;input type="checkbox" class="itemCheckbox">&lt;/td> &lt;td>2&lt;/td> &lt;/tr> &lt;tr> &lt;td>&lt;input type="checkbox" class="itemCheckbox">&lt;/td> &lt;td>3&lt;/td> &lt;/tr> &lt;/tbody> &lt;/table> &lt;script> $(function () { // 绑定 "全选" 功能到 #selectAll checkbox 上 $('#selectAll').on('click', function () { $('.itemCheckbox').prop('checked', this.checked); }); // 监听 tbody 内每一个 itemCheckbox 状态变化事件 $('tbody').on('click', '.itemCheckbox', function () { const allChecked = $('.itemCheckbox:checked').length === $('.itemCheckbox').length; // 设置 header 中 selectAll 的状态为 true/false if (allChecked) { $('#selectAll').prop('checked', true); // 所有都被选中了 } else { $('#selectAll').prop('checked', false); // 至少有一个没被选中 } }); }); &lt;/script> ``` --- ### 解释: #### (1)绑定全选逻辑: 我们通过监听 `#selectAll` 的 `click` 时间,在每次触发该事件的时候将其选中与否的状态同步给 `.itemCheckbox` 类的所有项。 ```javascript $('#selectAll').on('click', function () { $('.itemCheckbox').prop('checked', this.checked); }); ``` #### (2)监控子选项的变化并更新父级选项: 对于每个 `.itemCheckbox` 单独的操作情况我们也进行了监视。若当前页面上所有的 `.itemCheckbox` 都被选中,那么就将 `#selectAll` 自动打钩;只要存在一个没有被打钩的情况,就需要把 `#selectAll` 取消掉。 ```javascript $('tbody').on('click', '.itemCheckbox', function () { const allChecked = $('.itemCheckbox:checked').length === $('.itemCheckbox').length; if (allChecked){ $('#selectAll').prop('checked', true); }else{ $('#selectAll').prop('checked', false); } }); ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值