弘帝企业智能建站系统交流平台

 找回密码
 立即注册
查看: 3188|回复: 3

【jQuery】ajax利用serializer进行form传值

[复制链接]
发表于 2018-11-27 16:25:13 | 显示全部楼层 |阅读模式
同样的功能方法,可参考:http://wh.ac.cn/forum.php?mod=viewthread&tid=2736

ASP环境可用,利用$.extend追加json参数。
$("body").on("click","input[type=submit]",function () {
    var online = $("input[name=online]:checked").val();
    $.ajax({
        type: 'post',
        dataType: 'html',
        data: $.extend({ajax: 1,act: 'save',online: online},getFormData($('form'))),
        success: function (data) {
            console.log(data);
        }
    });
    return false;
})
//利用seriallize获取form值返回json。
function getFormData($form) {
    var unindexed_array = $form.serializeArray();
    var indexed_array = {};

    $.map(unindexed_array,function (n,i) {
        indexed_array[n['name']] = n['value'];
    });

    return indexed_array;
}

弘帝建站系统范例:login/js/online.js

ajax利用FormData进行form post传值,注意ASP环境Request.form不可用
http://wh.ac.cn/forum.php?mod=viewthread&tid=2736&fromuid=1



回复

使用道具 举报

 楼主| 发表于 2019-2-13 18:24:04 | 显示全部楼层

重写jQuery.param,表单提交使用serialize()方法解决中文乱码

ajax提交gbk乱码故障解决
POST调用方法:-->login/js/article.js
$("form[name=HD_Form]").on("submit", function () {
        $.ajax({
                type: 'post',
                dataType: 'html',
                data: $(this).serialize(),
                success: function (data) {
                        console.log(data);
                }
        });
        return false;
})

重写jQuery.param-->login/js/function.js
jQuery.param = function (a) {
        var s = [];
        var encode = function (str) {
                str = escape(str);
                str = str.replace(/%20/g, "%u002B");
                return str;
        };
        function add(key, value) {
                s[s.length] = encode(key) + '=' + encode(value);
        };
        // If an array was passed in, assume that it is an array  
        // of form elements  
        if (jQuery.isArray(a) || a.jquery)
                // Serialize the form elements  
                jQuery.each(a, function () {
                        add(this.name, this.value);
                });

        // Otherwise, assume that it's an object of key/value pairs  
        else
                // Serialize the key/values  
                for (var j in a)
                        // If the value is an array then the key names need to be repeated  
                        if (jQuery.isArray(a[j]))
                                jQuery.each(a[j], function () {
                                        add(j, this);
                                });
                        else
                                add(j, jQuery.isFunction(a[j]) ? a[j]() : a[j]);

        // Return the resulting serialization  
        return s.join("&").replace(/%20/g, "+");
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-29 21:46:39 | 显示全部楼层

【jQuery】$(this).serialize()添加额外参数

$("form[name=theForm").on("submit", function () {
    if (checkdata()) {
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: $.param({ ajax: 1 }) + '&' + $(this).serialize(),
            success: function (data) {
                if(data.stat==="ok"){
                    alert(data.msg);
                    //更新显示数据

                }else{
                    err(data.msg);
                }
            }
        });
    }
    return false;
});
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-16 11:42:40 | 显示全部楼层
还可以参考的方法:ew FormData($("form")[0]
网址:http://wh.ac.cn/forum.php?mod=viewthread&tid=2736


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|弘帝企业智能建站系统 ( 皖ICP备07503252号 )

GMT+8, 2024-4-24 06:28 , Processed in 0.096365 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表