form 表单提交方式


原文链接:http://www.anyrt.com/blog/list/submit.html

form表单提交方式

1.无刷新页面提交表单

表单可实现无刷新页面提交,无需页面跳转,如下,通过一个隐藏的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> 
              

2.通过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>
              

3.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();

4.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){
      }
   });
              

5.页面无跳转

如果通过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();
            }
        }
    }
              

6.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>                


提交表单之前验证表单内容:用onSubmit="return checkForm()"即可在form表单提交之前先执行checkForm函数,在函数里判断表单内容是否符合条件,并返回相应的值。若函数返回true,则提交表单,否则不提交表单。


7.<input type="submit"> 提交按钮提交表单。

例:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form action="" method="post" onsubmit="return checkForm()">
    <input type="text" name="username" id="name">
    <input type="text" name="password" id="password">
    <input type="submit" value="提交">
</form>
<script>
    function checkForm(){
        var name=document.getElementById("name").value;
        if(name==""||name==null){
            return false;
        }
        var pwd=document.getElementById("password").value;
        if(pwd==""||pwd==null){
            return false;
        }
        return true;
    }
</script>
</body>
</html>

8.<input type="button"> 按钮提交表单,button也可以触发表单的提交。

例:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form action="" method="post" onsubmit="return checkForm()">
    <input type="text" name="username" id="name">
    <input type="text" name="password" id="password">
    <input type="button" value="提交">
</form>
<script>
    function checkForm(){
        var name=document.getElementById("name").value;
        if(name==""||name==null){
            return false;
        }
        var pwd=document.getElementById("password").value;
        if(pwd==""||pwd==null){
            return false;
        }
        return true;
    }
</script>
</body>
</html>

    

9: $("#dataform").ajaxSubmit() 提交

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<form id="dataform" action="UpdateUserInfo" enctype="multipart/form-data" method="post">
<table style="width:100%;border:0;" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td width="40">名字:</td>
<td><input type="text" name="nvc_name" id="nvc_name" value="@Model.nvc_name" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="保存" id="btnsubmit" />
</td>
</tr>
</tbody>
</table>
</form>
<script src="http://malsup.github.io/jquery.form.js"></script> //ajaxForm 依赖脚本
<script type="text/javascript">
$(document).ready(function () {
$("#btnsubmit").click(function () {if ($("[name=‘nvc_name‘]").val() == "") {
alert("请填写名字");
$("[name=‘nvc_name‘]").focus();
return;
}
$("#dataform").ajaxSubmit(function (r) {
alert(r.Msg);
if (r.success) {
location.reload();
}
})
})
});
</script>

10:post 提交

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<table style="width:100%;border:0px;" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td width="15%" align="right">手机号:</td>
<td><input type="text"placeholder="请输入手机号" id="nvc_user_name"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="保存" id="btnsubmit" />
</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(function () {
$("#btnsubmit").click(function () {
var nvc_user_name = document.getElementById(‘nvc_user_name‘);
$.post(‘/Interface/ModefiyPwd‘, {
nvc_user_name: nvc_user_name.value,
}, function (data) {
if (data.success) {
$("#successdiv").show();
$("#editdiv").hide();
}
else {
layer.msg(data.Msg);
}
});
});
})
</script>


相关文章:

  • 2021-11-19
  • 2021-11-19
  • 2021-11-19
  • 2021-11-27
  • 2021-11-08
  • 2022-12-23
  • 2021-11-29
  • 2021-12-21
猜你喜欢
  • 2021-06-25
  • 2022-01-04
  • 2022-12-23
  • 2021-09-20
  • 2021-12-21
相关资源
相似解决方案