• Form表单提交数据的几种方式


    一、submit提交

    在form标签中添加Action(提交的地址)和method(post),且有一个submit按钮(<input type=’submit’>)就可以进行数据的提交,每一个input标签都需要有一个name属性,才能进行提交。

    <form action=’http://www.123.com/postValue’ method=’post’>

    <input type=’text’ name=’username’ />

    <input type=’password’ name=’password’/>

    <input type=’submit’ value=’登陆'/>

    </form>

    当点击登陆时,向服务端发生的数据是:username=username&password=password.

    这种默认的提交方式,一般会进行页面的跳转(不成功时跳转到当前页面)。而有时候我们是对弹出框进行数据提交的,希望提交成功则关闭弹出框并刷选父页面,失败则提示失败原因,且弹出框不关闭。此时可以采用Ajax进行数据提交.

    二、Ajax提交form表单

    $('#documentForm').submitForm({
                url: "/Document/SubmitDocumentCreate",
                dataType: "text",
                callback: function (data) {
                    endFileUpload();
                    data = eval("(" + data + ")");
                    alert(data.Content);
                    if (data.Result > 0) {
                        location.href = data.Redirect;
                    }
                },
                before: function () {
                    startFileUpload();
                    var errMsg = "";
                }
            }).submit();

    此时可以在callback函数中对请求结果进行判断,然后执行不同的动作(页面跳转或刷选数据、提醒错误都可以)

    三、Easyui的form插件

    通过easyui的form插件也可以达到上面的目的。

    $('#ff').form('submit', {

    url:...,

    onSubmit: function(){

    //进行表单验证

    //如果返回false阻止提交

    },

    success:function(data){

    alert(data)

    }

    });

    四、form表单提交附件

    需要设定form的enctype="multipart/form-data"并且添加<input type=’file’>

    而且附件只能通过submit方法进行提交,

  • 相关阅读:
    TypeScript 第一讲 ———— 基本数据类型的使用
    关于TypeScript命名空间
    Egret 自定义皮肤 ———— 引入类中以及createChildren()和 childrenCreated()的使用
    egret基础——控件
    回顾过去,展望未来
    JDBC、Hibernate、Mybatis之间的区别
    SSM框架优缺点和spring boot 比起优缺点是什么?
    拦截器和过滤器的区别
    转发和重定向区别
    关于虚拟机中克隆的linux为什么不能开启网络服务
  • 原文地址:https://www.cnblogs.com/Jxwz/p/4509618.html
Copyright © 2020-2023  润新知