使用表单标签 与用户交互

本文详细介绍了HTML表单的各种元素及用法,包括文本输入框、密码框、文本域、单选框、复选框、下拉列表框以及按钮等,并解释了如何通过表单收集用户数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网站怎样与用户交互?答案是使用HTML中的表单(form), 表单可以把浏览者输入的数据传送到服务器端,这样服务器端就可以处理表单传送的数据。

语法: <form method = "传送方式" action = "服务器文件">

讲解:1.<form>是成对出现的, 以<form> 开始, 以 </form>结束,表单都必须放在其之间。

  2.method 传送方式,  get/post  是后端程序员考虑的问题

  3.action  浏览者输入的数据被传送到的地方,比如一个php页面, (save.php)  

<form    method="post"   action="save.php">
        <label for="username">用户名:</label>
        <input type="text" name="username" />
        <label for="pass">密码:</label>
        <input type="password" name="pass" />
</form>


文本输入框,密码输入框

当用户需要在表单中键入字母,数据等,就要用到文本输入框,文本输入框也可以转化为密码输入框

语法:

<form>
    <input type = "text/password" name = "名称" value = "文本" />
</form>

讲解:1.type :

当 type 为 text时,为文本输入框

当 type 为 password 时, 为密码输入框

2.name :为文本框命名,以备后台asp php使用

3.value :为文本输入框设置默认值(一般起提示作用)

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>文本输入框、密码输入框</title>
</head>
<body>
<form  method="post" action="save.php">
    账户:
    <input type = "text" name = "myName" />
    <br />
    密码:
    <input type = "password "  name = "pass"/>
</form>
</body>
</html>
结果:

账户:  
密码:  

文本域:支持多行文本输入

当用户需要在表单中输入大段文字时,就要用到文本输入域

语法:

<textarea rows = "行数" cols = "列数" > 文本  </textarea>

讲解:1.文本输入域以 <textarea>开始 ,以 </textarea>结束

2.rows: 输入文本输入域的行数

3.cols : 输入文本输入域的列数

4.在<textarea></textarea>标签之间输入默认值

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>文本域</title>
</head>
<body>
<form method = "post" action = "save.php">
    <label>个人简介</label>
    <textarea rows = "5" cols = "10">在这里输入内容...</textarea></span>
    <input type = "submit" value = "确定" name = "submit" />
    <input type = "reset" value = "重置" name = "reset" />
</form>
</body>
</html>

结果:

   

<lable>在后面会有详解。


使用单选框,复选框让用户选择

在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好办法,在HTML中,有单选框和复选框,两者的主要区别是 单选框中用户的选项只能选择一项,而复选框中用户可以任意选择多项,甚至全选。

<input type = "radio/checkbox" value = "值" name = "名称" checked = "checked" />
讲解:

1. type : radio :控件单选框

    checkbox : 控件复选框

2. value: 提供数据到服务器的值

3. name:为控件命名,以备后台程序ASP,PHP使用

4.checked: 当设置 checked = “checked”时,该选项被默认选中。

 

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>单选框、复选框</title>
</head>
<body>
<form name = "iForm"  method = "post" action = "save.php">
    你是否喜欢旅游?<br />
    <input type = "radio" name = "radioLove" value = "喜欢" checked = "checked"/></span>
    <input type = "radio" name = "radioLove" value = "不喜欢"/>
    <input type = "radio" name = "radioLove" value = "无所谓"/>
    <br /> <br />
    你对那些运动感兴趣?<br />
    <input type = "checkbox" name = "checkbox1" value = "跑步"/>
    <input type = "checkbox" name = "checkbox1" value = "打球" checked = "checked"/>
    <input type = "checkbox" name = "checkbox1" value = "登山" checked = "checked"/>
    <input type = "checkbox" name = "checkbox1" value = "健身" />

</form>
</body>
</html>

结果:

你是否喜欢旅游?
   

你对那些运动感兴趣?
   


注意:同一组的单选按钮,name的取值一定要一致,这样同一组的单选按钮才可以起到单选的作用。



下拉列表框

使用下拉列表框,节省空间,既可以单选,又可以多选。

单选:


<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>下拉页表框</title>
</head>
<body>
<form name = "iForm"  method = "post" action = "save.php">
    <label>爱好:</label>
    <select>
        <option value = "读书">读书</option></span>
        <option value = "运动">运动</option>
        <option value = "音乐">音乐</option>
        <option value = "旅游">旅游</option>
        <option value = "购物">购物</option>
    <span style="color:#ff0000;"></select></span>

</form>
</body>
</html>
结果: 
  •          
  •          
  •          
  •          
  •          
  •        (可以下拉)

    <option value =  "提交值" > 选项</option>
    提交值:是向服务器提交的值

    选项:是显示的值

    设置 selected = "selected" 则该选项默认被选中。


    多选:

    就将上面的<select> 改为 <select multiple = "multiple" >就行,然后在widows下ctrl ,同时单击,在Mac 下 Command + 单击 



    使用提交按钮,提交数据

    在表单中有两种按钮可以使用,提交按钮和重置,当用户需要提交信息到服务器时,需要用到提交按钮。

    语法:

    <input type = "submit" value = "提交">

    讲解:

    1.只有当type = "sumit"时,按钮才有提交的作用

    2.value: 按钮上显示的字


    重置 

     当用户需要重置表单信息到初始状态时,可以使用重置按钮,只要把type 改为 reset 就行。

    <input type = "reset" value = "重置">

    讲解:

    1.同理提交按钮,只有当type = "reset"时, 按钮才有重置的作用

    2.value : 按钮上显示的文字


    label标签

    label标签不会向用户呈现任何特殊的效果,它的作用是为鼠标用户改进了可用性,如果你在label标签内点击文本,就会触发此控件,也就是说,当用户单击选中该label标签时,浏览器会自动将焦点转到和 标签相关的表单控件上(就自动选中和该label标签相关联的表单控件上);

    语法:

    <label  for = "控件id 的名称" >

    注意:标签中for 属性的值应该与相关控件的id属性值一定要相同;

    <form>
      <label for="male">男</label>
      <input type="radio" name="gender" <span style="color:#ff0000;">id="male"</span> />
      <br />
      <label for="female">女</label>
      <input type="radio" name="gender" <span style="color:#990000;">id="female"</span> />
      <label for="email">输入你的邮箱地址</label>
      <input type="email" <span style="color:#ff6666;">id="email"</span> placeholder="Enter email">
    </form>

    结果:

      
      
     

    以下是自己仿写的,可复选的:

    <!DOCTYPE HTML>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>form中的lable标签</title>
    </head>
    
    <body>
    <form>
        你对什么运动感兴趣:<br/>
        <label for = "sport1">慢跑</label>
        <input type = "checkbox" name = "sports" id = "sport1"/>
        <br />
        <label for = "sport2">登山</label>
        <input type = "checkbox" name = "sports" id = "sport2"/>
        <br />
        <label for = "sport3">篮球</label>
        <input type = "checkbox" name = "sports" id = "sport3"/>                     <br />
    </form>
    
    </body>
    </html>
    
    结果:

    你对什么运动感兴趣:
      
      
      

     

  • 评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值