zoukankan      html  css  js  c++  java
  • HTML form 表单

    HTML 表单用于搜集不同类型的用户输入。

    <form> 元素定义 HTML 表单:

    类型描述
    text 定义常规文本输入。
    radio 定义单选按钮输入(选择多个选择之一)
    submit 定义提交按钮(提交表单)
    password 密码
    select  下拉列表
    option  定义待选择的选项
    textarea  定义多行输入字段
    button  定义可点击的按钮
    datalist 为 <input> 元素规定预定义选项列表
    fieldset 组合表单中的相关数据
    legend 为 <fieldset> 元素定义标题
    checkbox 统计选中复选框的个数
    reset 重置表单
    hidden 定义隐藏字段
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Order Form</title>
    </head>
    <body>
        <h1>Order Form</h1>
        <form method='post'>
        <table>
            <tr>
                <td>Name:</td><td><input  name='name' /></td>
            </tr>
            <tr>
                <td>Address:</td>
                <!--文本区域  会将文字发送给服务器,如若没有任何内容,则会发送空字符""给服务器 -->
                <td><textarea name="address" id="" cols="40" rows="5"></textarea></td>
            </tr>
            <tr>
                <td>Country:</td>
                <!--  下拉框select 如果被选择,会将option选项的文字发送给服务器,不会向服务器返回任何值,  getParameter()为null -->
                <td><select name='country'>
                <option>Canada</option>
                <option>United States</option>
                </select></td>
            </tr>
            <tr>
                <td>Delivery Method:</td>
                <!--  radio 如果没有选择,不会向服务器返回任何值,  getParameter()为null-->
                <td><input type="radio" name="delivery" id="" value="First Class" />First Class
                <input type="radio" name="delivery" value="Second" />Second Class
                </td>
            </tr>
            <tr>
                <td>Shipping Instructions:</td>
                <!--  多个区域不输入内容,默认发送个<br> -->
                <td><textarea name="instruction" id="" cols="40" rows="5"></textarea></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><textarea name="instruction" id="" cols="40" rows="5"></textarea></td>
            </tr>
            <tr>
                <td>P    lease send me the latest product catalog:</td>
                <!--  单选框checkbox 将被选中按钮的值发送到服务器,如果没有选择任何按钮,将没有任何内容被发送到服务器
                  getParameter()为null-->
                <td><input type="checkbox" name="catalog" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="reset" /> <input type="submit" /></td>
            </tr>
        </table>
        </form>
    </body>
    </html>

    下面是 <form> 属性的列表:

    属性描述
    accept-charset 规定在被提交表单中使用的字符集(默认:页面字符集)。
    action 规定向何处提交表单的地址(URL)(提交页面)。
    autocomplete 规定浏览器应该自动完成表单(默认:开启)。
    enctype 规定被提交数据的编码(默认:url-encoded)。
    method 规定在提交表单时所用的 HTTP 方法(默认:GET)。
    name 规定识别表单的名称(对于 DOM 使用:document.forms.name)。
    novalidate 规定浏览器不验证表单。
    target 规定 action 属性中地址的目标(默认:_self)。
    checked 规定在页面加载时应该被预先选定的 input 元素
     

    form表单提交方式

    无刷新页面提交表单

    表单可实现无刷新页面提交,无需页面跳转,如下,通过一个隐藏的iframe实现,form表单的target设置为iframe的name名称,
    form提交目标位当前页面iframe则不会刷新页面

    <form action="/url.do" method="post" target="targetIfr">
    <input type="text" name="name"/>
    </form>   
    <iframe name="targetIfr" style="display:none"></iframe> 
                  

    通过type=submit提交

    一般表单提交通过type=submit实现,input type="submit",浏览器显示为button按钮,通过点击这个按钮提交表单数据跳转到/url.do

    <form action="/url.do" method="post">
       <input type="text" name="name"/>
       <input type="submit" value="提交">
    </form>
                  

    js提交form表单

    js事件触发表单提交,通过button、链接等触发事件,js调用submit()方法提交表单数据,jquery通过submit()方法

    <form id="form" action="/url.do" method="post">
       <input type="text" name="name"/>
    </form>
                  

    js: document.getElementById("form").submit();
    jquery: $("#form").submit();

    ajax异步提交表单数据

    采用ajax异步方式,通过js获取form中所有input、select等组件的值,将这些值组成Json格式,通过异步的方式与服务器端进行交互,
    一般将表单数据传送给服务器端,服务器端处理数据并返回结果信息等

    <form id="form"  method="post">
       <input type="text" name="name" id="name"/>
    </form>
      var params = {"name", $("#name").val()}
     $.ajax({
          type: "POST",
          url: "/url.do",
          data: params,
          dataType : "json",
          success: function(respMsg){
          }
       });
                  

    页面无跳转

    如果通过form表单提交请求服务端去下载文件,这时当前页面不会发生跳转,服务端返回void,通过response 去写文件数据,
    页面会显示下载文件。

    <form action="/url.do" method="post">
       <input type="text" name="name"/>
       <input type="submit" value="提交">
    </form>
    
    @RequestMapping(value = "/url")
        public void exportFile(HttpServletRequest req, HttpServletResponse response, String rptId)
                throws Exception {
            OutputStream out = null;
            try {
                String rptName = "file";
                String fileName = new String((rptName + excelAble.getFileSuffix()).getBytes("GBK"),
                        "8859_1");
                response.reset();
                response.setContentType("application/octec-stream");
                response.setHeader("Content-disposition", "attachment; filename=" + fileName);
                out = response.getOutputStream();
                excelAble.exportFile(out);
            } catch (Exception e) {
                logger.error(e);
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
                  

    form表单上传文件

    使用form表单进行上传文件需要为form添加enctype="multipart/form-data" 属性,除此之外还需要将表单的提交方法改成post,
    如下 method="post", input type的类型需要设置为file <form action="/url.do" enctype="multipart/form-data" method="post">

         <input type="file" name="name"/>
         <input type="submit" value="提交">
       </form>

    form 下列菜单select

    <select name='titleFontSize' > 
    <option value=large</option>
    <option >x-large</option>
    <option >xx-large</option>
    </select>

     form 多选菜单 select multiple

    <td>Title Style & Weight: </td>
    <td><select name='titleStyleAndWetght'multiple>// multiple='multiple' size='3'
    <option value="">italic</option> //当value指定了值时(""也算值),浏览器向服务器发送value的值,否则发送选选项italic或bold的值
    <option value="">bold</option>
    </select>

    form 提交按钮

    <input type="submit" value="set" />

  • 相关阅读:
    elementUI使用el-tabs时有个坑
    滚动条滚动到指定位置(锚点)的不同实现方式
    elementUI组件 el-checkbox 的值格式问题
    前端常见面试题(七)ajax
    this指向 以及 call、apply、bind的使用和区别
    vue 项目目录解释
    vue-lic
    vue 网络请求 axios
    vue 状态管理 vuex
    keep-alive+vue 路由 (vue-router)
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10658647.html
Copyright © 2011-2022 走看看