HTML表单

时间:2021-08-24 13:57:19   收藏:0   阅读:22

1. 创建表单

每个表单都以 form 开始标签开始,以 form 结束标签结束。两个标签之间是组成表单的说明标签、控件和按钮。每个控件都有一个 name 属性,用于在提交表单时对数据进行识别。form 开始标签可以有一些属性,其中最重要的就是 action 和 method。将 action 属性的值设置为访问者提交表单时服务器上对数据进行处理脚本的 URL。method 属性值要么是 get,要么是 post。大多情况下是 post。

  <form method="post" action="xxx.php">
  </form>

对表单元素进行组织:如果表单上有很多信息需要填写,可以使用 fieldset 元素(类似一个边框)将相关的元素组合在一起,使表单更容易理解。还可以使用 legend 元素为每个fieldset 提供一个标题,用于描述每个组的目的(有时还可以使用 h1~h6)。

<form action="">
<fieldset>
    <h1>...</h1>
</fieldset>

<fieldset>
    <h2>...</h2>
    <div>
        <fieldset><div class="">
            <legend>小标题</legend>
        </div></fieldset>
    </div>
</fieldset>

</form>

2.创建文本

 

文本框可以包含一行无格式的文本,它可以是访问者想输入的任何内容,通常用于姓名、地址等信息。每个文本框都是通过带有 type = "text" 的 input 标签表现出来的。除了 type 之外还有些其他可用的属性,其中最重要的就是 name。服务器端的脚本使用你指定的 name 获取访问者在脚本文本框中输入的值或预设的值(即 value 属性值)。

创建文本框步骤:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 提交表单   Last Name:

   <fieldset>
  <legend> 提交表单 </legend>
  <label for = "idlabel"> Last Name: </label>
  <input type = "text" id = "idlabel" name = "dlabel" class = "field-large"
   required autofocus />
   </fieldset>

让 for、id 和 name 属性值都一样是一种并非必须但是很常见的做法(单选框和复选框例外,对它们来说有一组 input 会使用同一个 name,而 id 对每个 input 来说都是唯一的)。

3.创建密码框

创建密码框的步骤和创建文本大致相同,唯一区别的地方在于 type 的值变为 type = "password"。密码框中输入的文本会使用圆点或星号隐藏。

 <fieldset>
  <legend> 提交表单 </legend>
 <label for = "idlabel"> Last Name: </label>
 <input type = "password" id = "idlabel" name = "dlabel" class = "field-large"
   required autofocus />
 </fieldset>

创建电子邮件框、搜索框、电话框和URL框

同创建文本的步骤相同,唯一区别的地方在于type属性值的变化。

电子邮件:type = "email";
搜索框:type = "search";
电话框:type = "tel";
URL框:type = "url";

4. 创建单选按钮

 

对 input 元素设置 type = "radio" 即可创建单选按钮。然后输入 name 和 value 值,如果需要输入 checked 或者 checked = "checked" 让该单选按钮在页面打开时默认处于激活状态。一组单选按钮只能有一个按钮添加该属性。

Male

     <input type = "radio" name = "gender" value = "Male" />
     <label for = "idgender"> Male </label>

5.创建复选框

在一组单选按钮中,只允许选择一个答案;但在一组复选框按钮中,访问者可以选择任意数量的答案。

It‘s okay to ...emali me with ...messages from other ...

 <input type = "checkbox" id = "email- ok" 
 name = "email - signup" value = "user- emails" />
<label for = "email- ok" > It‘s okay to ...</label>
  
   <input type = "checkbox" id = "email-ok- to" 
   name = "email - signup" value = "user- emails- to" />
 <label for = "email- ok-to" > emali me with ...</label>
 
  <input type = "checkbox" id = "email- ok- three" 
  name = "email- signup" value = "user- emails- three" />
 <label for = "email- ok- three" > messages from other ...</label>

6.创建文本区域:

 

如果希望填写问题或评论的空间,可以使用文本区域。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原文:https://www.cnblogs.com/sam-zh/p/15179668.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!